How to create a webservice in asp.net ?

The reason for this blog is,I am getting so many question regarding the webservices from so many people  okey , let us see how to create a webservice in asp.net? how to protect a webservice? how to invoke a webservice? How to deploy webservices in IIS ? In my early stage even I thought  that it is a very big deal to work with webservices. to be frank It is nothing 🙂 . And many of the developers are having the question in their mind, where to start with webservices ?It is a very simple article which I am providing here along with the code.
1) Where to start ?

Install WSE3.0 in you machine (download it from http://www.microsoft.com/en-us/download/details.aspx?id=14089)

open Microsoft visual studio-> take new Asp.net website -> solution explorer ->add new item->add webservices(by default it will be “Webservices.asmx”)

add this below given code in your web.config.

<webServices>
<soapExtensionTypes>
<add type=”Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ priority=”1″ group=”0″/>

</soapExtensionTypes>
<protocols>
<add name=”HttpSoap”/>
<add name=”HttpPost”/>
<add name=”HttpGet”/>
<add name=”Documentation”/>
</protocols>
</webServices>

your webservice skeleton is ready 🙂

2) How to work ?

By default you will be getting a webmethod “HelloWorld” in your Webservices.cs; like this you can have n number of webmethods .Webservices.cs is a normal class file ,so you can implement your custom logic here. I am not entering into the logical part .

3) How to test the webservice ?
once you did the coding next part is testing the webserices .
Go to solutionExplorer -> right click on the webservices.asmx file -> set as a start up page-> run the application

In browser you can see all the public method which you added in Webservices.cs -> click on the webthod-> here you can see you soap structure , then click on the invoke method -> it will return the value

Now your webservice is ready 🙂

If you want to see the wsdl of your webservice, then in the browser pass the querystring (as wsdl) like this “http://localhost:1556/WebSite1/WebService.asmx?wsdl” (**your** webservice.asmx?wsdl). This wsdl only will be using in the time of consuming the webservice.


4)how to deploy webservices ?

Go to solution explorer-> right click on the website-> click on publish website (remember the path where you are deploying the website 🙂 )
open IIS->create a virtual directory with browse permission (path will be the same as where you published your website). Give proper permission for the published folder also.

Try to invoke the webmethod -> it will work fine 🙂

Now you are created the webservice successfully :). Next stage is consuming the webservice.

5)How to consume a webservice?

Suppose a person X had created the webservice (X person might be from 3rd party),and he told you that “I created the webservice; you can use this url for accessing it (htttp://xxxxxx.com/ws/WebService.asmx) ; okey fine 🙂

let us see how to consume this webservice?First make sure that whether this given url is accessible, if no contact the person and ask them to provide the proxy class.If yes , well and good we can do it 🙂

For consuming the webservices we have mainly two ways a) adding webreference b)create a proxy class and access the webservice.
I prefer the second way the reason is that, here you will be having the full control on the proxy.You can use the same proxy class in development ,testing, UAT even in production  environment by just changing the url(“this.url” from the proxy class)

6)How to generate a proxy calss?
refer : http://dotnetstock.com/technical/how-to-generate-proxy-class-from-wsdlasmx

okey, now we have proxy class with us .Proxy class are nothing but a normal class, but it will contain all the relevant information about the webservice which you are going to be used. It will be having the webnethods, return type ,input and output parameter,  properties etc.

Now you can create an object for the proxy class from your application and you can invoke the webmethods from the there.

sample code for accessing web methods from class

WebService WS = new WebService();
string str = WS.HelloWorld();
int a = WS.test();
Response.Write(a+str);

cool 🙂 we are consumed the webservices also :):)

thank you !!!!

Leave a Reply

Your email address will not be published. Required fields are marked *