Wednesday, March 3, 2010

C# - Example to create web service

Below is an example to create / utilize Web Service:
1) By using Visual Studio 2008, select to create a project of "ASP .NET Web Service" using C#

2) Change namespace to Microsoft as below:
[WebService(Namespace = "http://microsoft.com/webservices/")]

3) Right Click file "Service1.asmx", select "View in Browser" to test functionality of the "Web Service".

4) Right Click the project file, select "Rebuild", "Convert to Web Application" and then "Pubish" to publish the web service to proper Virtual Directory of IIS.

5) For the Virtual Directory of the folder which stores the Web Service, right click to sleect "Property" and then click button "CReate" to make the directory to support "Application".

These complete the building and publishing of a web service.

6) Use Visual Studio to create aWeb Form application. Click to select "Add web reference" and declare URL as below to add the service for usage:

http://server_ip/xxx/yyy/zzz/Service1.asmx?op=HelloWorld

7) Add code as below to use the "Web Service":

{ ....

WebReference.Service1 service = new WebReference.Service1();
string message = service.HelloWorld();
MessageBox.Show(message);
....
}

No comments: