Related Posts with Thumbnails

Web Services

Monday, May 3, 2010

Webservices are services exposed over the internet. Typically, webservice is just like any other class library, written in any language. What make it a 'web service' is, it can be accessed across internet.

Eventhough webservice is a new technology with a wide range of usage, it is a pretty simple concept. It doesn't require much additional knowledge to create web services if you are already familiar with C# or VB.NET. (Web services are not specific to .NET. Even Java has web service applications, but here we are discussing only the .NET web services.)

As we mentioned, web services are exposed over internet. To make this happen, it has to be hosted with a web site. Othen than hosted as part of a web site to make this visible across internet, web services have no web pages or UI. It is just a set of classes with public/private methods and properties.

Web services are a group of [web] methods. Consumer applications can treat this as just another class library. Regular class libraries are located along with the same application and we can call any methods in the class libraries (assemblies). But in case of web services, the assembly is located in the internet server. The consumer application will not have direct access to the assembly. Consumer applications have to add a reference to the webservice URL and then call methods in it. The method call goes across the internet using SOAP protocal and results are returned across internet in the form of XML. The communication across internet happens transparently and the application need not know anything about this communication.

Creating a simple web service

If you are using VS.NET, it won't take more than 2-3 minutes to create your first webservice. Follow the steps below to create a web service:






  • Choose New Projectfrom VS.NET and create a C# project of type 'ASP.NET Web Service' and give the name 'MyWebService'













  • The new project will have a default page 'Service1.asmx'. This is your first web service page. Web service pages have the extension .asmx (like ASP.NET pages have the extension.aspx)













  • The code behind file 'Service1.asmx.cs' have a class 'Service1'. This is like other regular classes with the only difference that it inherits the system classSystem.Web.Services.WebService. This is required for web service classes.













  • VS.NET creates a sample method for you. All you have to do is, uncomment the following method in Service1.asmx.cs 





    //[WebMethod]
    //public string HelloWorld()
    //{
    //return "Hello World";
    //}
    Note that this method is pretty much same as any other regular method.
    The only difference is, it has an attribute [WebMethod]. This attribute make
    the method visible across internet. If you remove this attribute, your
    application will still compile, but the consumer applications cannot call this
    method across internet.

    Ok, thats all you have to do to create a simple webservice. Build your solution and your web service is ready to be called by other applications.

    Create a consumer application

    A wide range of applications can consume the web services, including web page, other web services, windows applications etc. let us create a sample windows application and call our web service.













  • Create a new C# Windows Application.













  • Right click on the 'References' in the Solution Explorer, under your project name. Choose 'Web References'.













  • Another window will open and will allow you to specify the URL of the web service to be referenced.

    Type the URL of your web service in the space provided for 'URL'. If you have created the web service in the local machine's default web site, the URL might be like this :







    http://localhost/MyWebService/Service1.asmx
    If you have given a different name fro the project or if you have changed the default service name, then your URL may differ. Type the URL and press enter. It will attempt to connect with the web service and if succeeded it will retrieve the public web methods and display. Once you get this success screen, press the 'Add Reference' button on the screen to add the web reference to your application. There is a space to specify the 'web reference name', which will have the default value 'localhost'. Leave it as it is.

    Now you are ready to make calls to the webservice. Just use the following code in your application: 





    localhost.Service1 service = new localhost.Service1();
    string result = service.HelloWorld();
    
    MessageBox.Show( result );
    Let us analyze our code. The first line creates an instance of the Service1 class, just like any regular class. Only thing is it uses the namespace 'localhost'. This is same as the 'web reference name' we specified while adding the web reference. We can use any web reference name when we add the reference and have to use the same name as namespace while making calls to the web service class.

    In the second line, we are actually calling the web method 'HelloWorld()' and it returns the result as a string. Note this is just like any other method call. So, your application makes the call to web service as if it is calling any local class library calls!! The communication with web service, wrapping your calls in SOAP/XML, retrieve results from web service, unwrap the SOAP/XML to get the actual result etc are done behind the scene by the framework and you need not do any additional work to make all this work.

    Now you can go back to your web service, add more methods with different parameters and play around with that. You will be able to call methods in the web service just like you use any local class. Note that when you make any changes to the method name or parameters, or if you add new methods, you will need to refresh your web reference to get access to the updated web service methods. To do this, go to the Server Explorer, right click on the web reference name 'localhost' under the 'Web References'. Then choose 'Refresh'. This will communicate with the web service again and retrieve the latest web service data.

    How does all this work? 

    To make calls to any classes, your application need to know the details of the class. To successfully compile your code, it has to have information about the class, what are the public methods and properties etc. But in your sample code which calls the web service, the actual Service1class resides in the web (in your case, in your local web server). So how does the application compile without having the 'Service1' class in your application?

    This is where the framework help you. When you add a 'web reference' to the web service URL, VS.NET actually creates local proxy in your application. A local proxy is a minimal version of the actual class. The proxy has just the sufficient information about the 'real class', which is required for your application to successfully compile. The proxy class has the list of all public web methods in the real web service class and it knows the parameters and their data types of each method.

    The most important thing is, your application is actually using this proxy class and not the real web service class. 





    localhost.Service1 service = new localhost.Service1();
    string result = service.HelloWorld();
    
    MessageBox.Show( result );
    In the above code, localhost.Service1is actually the proxy class (a representative of a real class), which is created automatically by VS.NET when you added the web reference. You are creating an instance of the proxy class and calling the 'HelloWorld()' method of this proxy. Since the proxy class knows that it is 'only a proxy of the real class', it redirects all calls to the 'real web service class'. The method 'HelloWorld()' in the proxy class takes care of converting the call to a web request using SOAP/XML. After retrieving the result of the web request to the real web service class, this proxy class takes the responsibility of parsing the SOAP/XML response and returning only the result string to the calling applicationn. So the proxy class does all the dirty job for you.

    The proxy class has no implementation of the functionality. It just redirects the call to the web service. So, if you make any changes to the implementation in the web service, still you will get the 'updated result' when you make calls to web service. But, if you change the method name, or change the parameter types in the web service and do not 'refresh the web reference', then your application will compile with the old proxy. But when the proxy redirect the call to the web service, it will find that the parameters or method names do not match and it will fail. So, if there is any change in the method names or parameters, you must refresh your web reference.

    We will explore more features of web services in another chapter. Please complete the following feedback, if this article is incomplete or misleading. Please let us know if there is anyway we can improve this article.









  • You can download web service samples from our projects section.

    0 comments:

    Post a Comment

    Site Rate