Wednesday 19 September 2012

How to create web services in asp.net


Hi,today i am going to explain about creating simple web services in asp.net. i hope it will help for beginner's of asp.net.i am going to explain step-by-step manner ,just fallow the lines what i have written below. 

  • Web services are web applications only, they are providing services to some other applications over the Internet.
Eg: All Search Engines, Currency Converters
  • Web service is one of the type of application, it is depending on Internet connectivity to function that’s why we are calling it as a web application
  • It is an application which is providing services through web, that’s why we are calling it as web services

  • In general web services implementation will be divided into 2 phases:
Phase 1:  Creating a web service according to our requirement
Phase 2:  Creating client application to consume the web service.

                        Here web service is a service provider; client application is a service receiver.
beginer
Purpose of Web Services:

1)      Reusing the Component: Whenever we are using the same component in multiple applications instead of creating the same component in all the applications, we will create a web service for that component and we will consume that web service from the ‘n’ no. of applications.

Eg: Consuming the currency converter from multiple website

2)      Exchanging the data between applications: Whenever we want to exchange the data between two applications we will use the web services.

A web service can consumed by any type of application like Console, Windows, Web application,..etc.,

In .Net if we want to develop the web services we have a template called “ASP .NET Web Service Application”

By default ASP.NET Web Service application is coming with a one class called Service1

Web service is a pre-defined class which is base class for the user defined service classess

                        Class service1 : web service
                        {
                                    ---
                        }

Web service class is a part of System.Web.Services name space

                        namespace services
                        {
                                    class webservices
                                    {
                                                ---
                                    }
                        }

In .Net web service file will be represented with .asmx

Web service will representing with platform independent component because it can provide services to all the type of applications over the network with respect to of operating systems.

This component is a collection of web methods

Web Method: The method which is providing services to other applications is called as Web method.

            A Web service class is a collection of web methods.


Working with Web Services:

                        It is divided into 2 phases:
           
            Phase 1: Creating a web service in step by step process
           
            Phase 2: Creating a client application and consuming the web service

Step by step for creating a Web Service:

Step 1: Open a Web Service application using ASP.NET Web application template

            Create Project à Visual Studio C# à ASP.NET Web application

-         Removing the existing “Hello World” method

Step 2: Implementing web methods according to our requirement [In this we use 2 methods Add( ) and Sub( )]

                        [ web method]

                        Public int Add(int a, int b)
{
            returen a+b;
}

[web method]

Public int sub(int a, int b)
{
            return a-b;
}

-         Build the application

Step 3:  Creating a client application
           
-         Open a windows application
o   Create project à Visual C# à Windows application à Ok

-         Design clientapps windows from.




Step 4: According the web service reference to client application

-         Goto Solution Explorer of the client application
o   Select References à Right Click à Add service reference à Ok
                       

-         Go back to Web services application window and run the web service application window
-         Here you may get a message box à Click Ok
-         Then it will open the web services page within the browser window
-         Goto the browser window address bar and Copy the url of the web service
-         Now go back to the client application window and Paste that url within the Add service reference window within the address bar.
-         Now click Ok

-         Now client application solution explorer window is representing a new file called Service references. It is representing the our web service.

Step 5: Consuming the web service

-         Gotot the client application code window, create object for web reference

Code within form_load( )

            Public partial class form1 : form
            {
                        Public form1( )
                        {
                                    Initialize component( );
                        }

Service reference1.Service1Soapclient serobj = new clientapp.servicereference1.service1Soapclient( );
Implement the btnAdd client event:

            Double click on Add button and write code as:

            Private void btnAdd_click( )
            {
textResult.Text =          serobj.Add(Convert.Toint32(textNum1.Text),
Conver.Toint32(textNum2.Text)).ToString( );\
                        }

            Implement the btnSub client event:

                        Double click on Sub button and write code as:

            Private void btnSub_click( )
            {
textResult.Text =          serobj.Sub(Convert.Toint32(textNum1.Text),
Conver.Toint32(textNum2.Text)).ToString( );\
                        }

-         Build the application and run the application

Ø  In .Net Web Services are integrated with XML, thatswhy we are calling it as XML web services
Ø  In Web services the data which we will use will be representing in the form of XML
Ø  The data will be coding and decoding from XML file
Ø  XML is a platform independent and language independent technology, automatically web services applications are also platform independent and language applications
Ø  In .Net web services are based on SOAP (Simple Object Access Protocol). Soap is communication protocol and it is called as open protocol. SOAP is based on XML. SOAP is acting as a mediator between .Net Web Service and Client application
Ø  That means the data transportation responsibility between Web Services and Client taking by SOAP

No comments: