Thursday, November 8, 2012

Build your own C# webservice Step by Step

First of all we need to know what is web services???


The W3C defines a "Web service" as "a software system designed to support interoperable machine-to-machine interaction over a network". It has an interface described in a machine-processable format (specifically Web Services Description Language, known by the acronym WSDL). Other systems interact with the Web service in a manner prescribed by its description using SOAP messages, typically conveyed using HTTP with anXML serialization in conjunction with other Web-related standards.(Source wikipedia),

Data transferring through web service more secure and reliable,by the help of web service we just call a method with some parameters and the methods return some result.


Here we discuss about asp.net web service with C#.
Step of building a web services : 
Step of building a web services : 
Step 1 : 
In visual studio 2008 and visual studio 2010 we can develop asp.net web services. We need dot net frame work 3.5 to build web services.
Create a new project like this 
Step 2. 
Sample code to do this
Sample code to do this
Now your web service is ready you can run it and check the newly created web services.
     Step 3 : 
    Now you can published your web service in the web solution project properties tab.








just look at the generated code there are already created a method named HelloWorld()

You  can change the method parameter as your requirement.
Here we will create a web method which will take two number as input and add them and return the result.
And create a method to check a number prime or not.
Note that you can change the namespace , this will never effect your operation,next discussion will clear this namespace.


   
[WebService(Namespace = "MYFIRSTWS")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
//methods for add two number
        [WebMethod]
        public int sumi(int x, int y)
        {
            return x + y;
        }
         //Methods for check prime

        [WebMethod]
        public bool isprime(int x)
        {
            for (int i = 2; i < x; i++)
            {
                if (x % i == 0)
                    return true;

            }

            return false;
        }
    }



And then published web services put in the IIS and access it from php or asp or CE desktop or java project. Our next post will discuss about the access of asp.net web services.


No comments:

Post a Comment