Now, we can use the HelloWorldService as we are currently using. Below, I provide a clear sample code. The ScriptManager label is also displayed. Note that we have added a clientServiceHelper. js file. We placed all JavaScript Functions for client-to-WCF communication 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 section of the clientServiceHelper. js file, we use an 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. policyscriptloaded ();
|
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 the 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 in the C # code. The sample code is as follows:
ScriptManager manager = ScriptManager.GetCurrent(Page); ServiceReference reference = new ServiceReference("ToDoService.svc"); manager.Services.Add(reference); |
- Use ASP. net ajax to call the WCF Service item Template
- How Ajax is implemented in ASP. NET
- Use Ajax and Web Service Development instances of ASP. Net 3.5