Web Service: Introduction
Web services extend the World Wide Web infrastructure to provide the means for software to connect to other software applications. Applications access Web services via ever present Web protocols and data formats such as HTTP, XML, and SOAP, with no need to worry about how each Web service is implemented. Web services combine the best aspects of component-based development and the Web, and are a cornerstone of the Microsoft .NET programming model.
What is Web Service?
Web services are a legacy technology. It enables an application to invoke method of another application. These applications can be on same computer or on different computers.
Web services always uses open standards and protocols so that applications built on any platform can easily interoperate with web services.
HTTP (Hypertext Transfer Protocol) widely used protocol to send or receive messages. SMTP can also be used.
SOAP (Simple Object Access Protocol) is the messaging protocol. It is in XML format.
File>New >project>empty web application>Add item>web service (.asmx file extension)
Web Service actually is a class with [WebService(Namespace= “https://msatechnosoft.com/webservice”)] attribute
It is inherited from System.Web.Services.WebService to access asp.net session state or application state objects directly.
In [WebService(Namespace= “https://msatechnosoft.com/webservice”)] namespace is used to uniquely identify your web service over the internet. When you publish your web service over the internet, there may be another web service with the same name but the unique namespace along with the name of web service will identify yours.
Method defined in web service is decorated with [WebMethod] attribute so that client application can see the method of your web application. If the method of web service will not have that attribute, client application will not be able to see that method. The method must be defined public as well. You can also customize your web method with the help of WebMethod properties.
Consuming a web service
Let us see how to consume a web service from client application.
Step1: generate a proxy class using WSDL document of web service
Run your web service in browser>click on service description>WSDL application
Copy the address of the WSDL document
Step2: go to client application > right click on reference> add service reference> paste the address of WSDL document in address bar> click on Go button >click on Ok
Visual studio will add a proxy class under Service References. Use the proxy class reference.cs in your client application.
WSDL (Web Service Description Language) is a document that define web service. It contain detailed description of all the web methods of web service. With the help of this document visual studio creates proxy class. Client application calls the methods of proxy class. Proxy class serialize the parameters and send SOAP request to web service. Web service then execute the method and return the result to proxy as SOAP response. Proxy class then de-serialize the response and display the result on client application.
Advantage of Web Services
- Web service is simple to build and supported on a wide range of platforms.
- Web service can extend its interface and add new methods without affecting the client’s operations.
- Web service is firewall friendly because all communication happens through HTTP on port 80.
- Web service are stateless, there is no permanent connection that scale up the many clients.
Use session state in web service
- Web service class must be inherited from System.Web.Services.WebService
public Class MyWebService : System.Web.Services.WebService
- EnableSession of WebMethod must be true.
[WebMethod(EnableSession = true)]
To update the web service, we need to update the proxy class in client application. To do so, right click on your web service reference folder inside reference folder and click on “Update service reference”.
“allowCookies” attribute must be set to true in client application to accept cookies returned from web services. This attribute is set in web.config file in client application. It ensures that the same session is maintained between the client and web service.
WebMethod attribute properties
This property is used to specify a description for web service method in service page.
It is a Boolean property that return the response of XML web service method to the client if set to false. The default value is true means it doesn’t return the response of xml web service method to client until the buffer is full or the response is completely serialized.
When xml web service method return small amount of data, its BufferResponse is set to true. When it return large amount of data, it is set to false so that the client does not require to wait until the whole response is serialized.
Cache is used to improve the performance of an application. This property is used if we want to keep cache of results of web service. It carries seconds as an integer value. It specifies the number of seconds that the response should be cached.
Overloading webMethods of WebService
MessageName property of WebMethod is used for method overloading in Web Service. This property uniquely identify web methods of xml web service.
- We are required to add ScriptManager control (under AJAX Extension) to web form from where we need to call the wen service. It is also required to specify the path of the web service within
<asp:ServiceReference Path= “~/myservice.asmx” />
Web Service belongs to Business Logic Layer that contain logic to validate business rules.
We can use AJAX that allows partial page post back that means we don’t need to reload the entire page to call a web service. Only specific part of the page will get reload. It not only avoid screen flicker but also improve performance.
for more informative and technology related post, visit our Tech-Blog