Now, you can use the HelloWorldService service as we now use it. Below, I give a relatively clear sample code to complete. The ScriptManager label is also displayed. Note that we have added a clientservicehelper.js file. We placed all the JavaScript functions for client and WCF communications in that file.
<asp:scriptmanager id= "ScriptManager1" runat= "Server" >
<Scripts>
<asp:scriptreference path= "~/script/clientserviceheler.js"/>
</Scripts>
<Services>
<asp:servicereference path= "~/todoservice.svc"/>
</Services>
</asp:ScriptManager>
In the Clientservicehelper.js file section, we used a asp.net AJAX
Client object model to write JavaScript client code.
Type.registerNamespace ("serviceclients");
Serviceclients.todoclient = function () {
}
ServiceClients.ToDoClient.prototype = {
Addtodo:function (Todo, callback, Errorcallback) {
ServiceLibrary.IToDoService.AddToDo (Todo, callback, errorcallback);
},
Deletetodo:function (Todo, callback, Errorcallback) {
ServiceLibrary.IToDoService.DeleteToDo (Todo, callback, errorcallback);
},
Updatetodo:function (Todo, callback, Errorcallback) {
ServiceLibrary.IToDoService.UpdateToDo (Todo, callback, errorcallback);
},
Getalltodo:function (callback, Errorcallback) {
ServiceLibrary.IToDoService.GetAllToDo (callback, Errorcallback);
},
Dispose:function () {
}
}
ServiceClients.ToDoClient.registerClass (' serviceclients.todoclient ', NULL, sys.idisposable)
if (typeof (Sys)!== ' undefined ') Sys.Application.notifyScriptLoaded ();
Summary
In this article, we have learned how to use the AJAX-enabled WCF service item template. Then we learned how to use a service interface based on a WCF service. We also learned how to configure the Web.config file to use the service. Finally, we learned how to add a service reference in ScriptManager. We can also add a service reference to the C # code. The sample code looks like this:
ScriptManager manager = ScriptManager.GetCurrent(Page);
ServiceReference reference = new ServiceReference("ToDoService.svc");
manager.Services.Add(reference);