Web Services Using C# - Creating Web Services

STEP 1

Open Visual Studio > File > New > Web Site…

C#

Step 2

Create “ASP.Net Empty Web Site” (name it“MyWebService”) and save at the desired location.

C#

STEP 3

Right-click on Project folder in Solution Explorer on the right side > Add > New Item. 

C#
 
Step 4

Select your language (C# in my case) > Web Service (ASMX) > Add. Now, name it (for example MyWebService.asmx).

C#

Explanation of the default code

C#

[WebService(Namespace = "http://tempuri.org/")]

Each XML Web service needs a unique namespace in order for client applications to distinguish it from other services on the web.

tempuri.org is the test default namespace URI. It is available for XML Web services that are under development, but published XML Web services should use a more permanent namespace. The term is strictly a placeholder and all instances of it should be replaced with a more meaningful URI in production systems.

An XML Web service should be identified by a namespace that is controlled by its company. For example, a company's Internet domain name could be used as part of the namespace. Although many XML Web service namespaces look like URLs, they need not point to actual resources on the Web.

  • Cannot have more than one [WebService] tag
  • Changing your namespace will have no effect on performance. It is just a unique identifier

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

Web service does not support method overloading directly. In order to allow overloading in the web services,

We have to add the MessageName property to make difference between methods and change

[WebServiceBinding(ConformsTo= WsiProfiles.BasicProfile1_1)] to [WebServiceBinding(ConformsTo = WsiProfiles.None)].

Example

  1. [WebMethod(MessageName = "twoparameter", Description = "addition of two integers")]  
  2. public int add(int a, int b) {  
  3.         return (a + b);  
  4.     }  
  5.     [WebMethod(MessageName = "threeparameter", Description = "addition of three integers")]  
  6. public int add(int a, int b, int c) {  
  7.         return (a + b + c);  
  8.     }  
  9.     [System.Web.Script.Services.ScriptService]  

To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

// [System.Web.Script.Services.ScriptService]

[WebMethod]

This attribute is used at the top of the method that you write in the Web Service. This is used to specify that this method is exposed to the user/Client to access.

If you remove this attribute, the client will not be able to see the method details, hence they can't implement it.

Proxy Class OR WSDL

Visual Studio generates a proxy class using the WSDL (Web Service Description Language) document of the web service. The WSDL document formally defines a web service. It contains 

  1. All the methods that are exposed by the web service
  2. The parameters and their types
  3. The return types of the methods

This information is then used by Visual Studio to create the proxy class. The client application calls the proxy class method. The proxy class will then serialize the parameters, prepare a SOAP request message and send it to the web service. The web service executes the method and returns a SOAP response message to the proxy. The proxy class will then deserialize the SOAP response message and hands it the client application. We don't have to serialize or deserialize dot net CLR objects to and from SOAP format. The proxy class takes care of serialization and deserialization and makes the life of a developer much easier.

In the next chapter, we will learn how to Consume Web Services in Client Application.


Similar Articles