Tuesday, May 19, 2009

Create Web Service in .Net

Hello Friends

Here sample code for how to create web service and consume int in C#.Net.

Step 1 : Add new Web Service(.asmx) file using add New Item
Step 2 : Suppose i have add Operation.asmx file
Step 3 :Add namespace of web service
///


/// Summary description for Operation
///

[WebService(Namespace = "http://localhost:3870/ConnectMySql/Operation.asmx")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
Step 4 : Create Function using [WebMethod]
//[WebMethod]
public int Addition(int a,int b)
{
int c;
c = a + b;
return c;
}
Step 5: Now build the web site and web service is created.

Now for Consume Web Service Step

Step 6: Add web service referance to another site using right click on project and add web referance.
Enter web service url http://localhost:3870/ConnectMySql/Operation.asmx and add namespace name for exa. "localhost".
Step 7 : Now add Namespace in page by
using localhost
Step 8 : Create object of Operation Class
Operation obj = new Operation();
int a = obj.Addition(5,2)
Response.Write(a.ToString());

Thanks & Regards

Jignesh Patel


No comments:

Post a Comment