Friday 14 June 2013

How to create and consume a .Net Wbeservice.





1)       creating a sample webapplication for host the webservice.





2) we are adding new item as webservice.




3)After adding webservice , change code like
public class SampleWebservice : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Thanks for calling web service";
        }
    }
And build the webapplication.
4) we can view webservice in browser to get the  url of  webservice.




5)  in address bar we can see the url of  helloworld webservice url

6)create a sample project to call webservice.
7)add the service reference as our webservice to the project.




8)after that paste the webservice address to the address bar and click on go. Then you can see your helloworld() method.below you can see the namespace field and you can write myservice or any name you like.



9)In winform add the below code:
 private SampleWebserviceSoapClient objclient = new SampleWebserviceSoapClient();
here I am calling webservice in a bnutton click.
private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = objclient.HelloWorld();
        }


10)Run the project and you can see the response in textbox.

2 comments: