ms.net

3
Microsoft .Net 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 Project from 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 classeswith the only difference that it inherits the system class System.Web.Services.WebService . Thisis required for web service classes.* VS.NET creates a sample method for you. All you have to do is, uncomment the followingmethod 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, ithas an attribute [WebMethod]. This attribute make the method visible across internet. If youremove this attribute, your application will still compile, but the consumer applications cannotcall this method across internet.Ok, thats all you have to do to create a simple webservice. Build your solution and your webservice 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 webservices, windows applications etc. let us create a sample windows application and call our webservice.* Create a new C# Windows Application.* Right click on the 'References' in the Solution Explorer, under your project name. Choose 'WebReferences'.* Another window will open and will allow you to specify the URL of the web service to bereferenced.Type the URL of your web service in the space provided for 'URL'. If you have created the webservice in the local machine's default web site, the URL might be like this :http://localhost/MyWebService/Service1.asmxIf you have given a different name fro the project or if you have changed the default servicename, then your URL may differ. Type the URL and press enter. It will attempt to connect withthe web service and if succeded it will retrieve the public web methods and display. Once you getthis success screen, press the 'Add Reference' button on the screen to add the web reference toyour application. There is a space to specify the 'web reference name', which will have the defaultvalue '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(); SUNSAT The Perfect Team 134 Microsoft .Net string result = service.HelloWorld();MessageBox.Show( result );Let us analyze our code. The first line creates an instance of the Service1 class, just like anyregular class. Only thing is it uses the namespace 'localhost'. This is same as the 'web referencename' we specified while adding the web reference. We can use any web reference name whenwe add the reference and have to use the same name as namespace

Upload: aravind-bhombore

Post on 07-Nov-2015

214 views

Category:

Documents


0 download

DESCRIPTION

ms

TRANSCRIPT

Microsoft .NetIf 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 aweb service:* ChooseNew Projectfrom VS.NET and create a C# project of type 'ASP.NET Web Service'and give the name 'MyWebService'* The new project will havea default page 'Service1.asmx'. This isyour first web service page.Web service pages have theextension.asmx(like ASP.NET pages have theextension.aspx)* The code behind file'Service1.asmx.cs' have a class 'Service1'. This islike other regular classeswith the only difference that it inherits thesystem classSystem.Web.Services.WebService. Thisis required for web service classes.* VS.NET creates a sample method for you. All you have to do is, uncomment the followingmethod 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, ithas an attribute [WebMethod]. This attribute make themethod visible across internet. If youremove this attribute, your application will still compile, but the consumer applications cannotcall this method across internet.Ok, thats all you haveto do to create asimple webservice. Build your solution and your webservice is ready to becalled by other applications.Create a consumer applicationA wide range ofapplications can consume the web services, including web page, otherwebservices, windows applications etc. let us create asample windows application and call our webservice.* Create anew C# Windows Application.* Right click on the'References' in the Solution Explorer, under your project name.Choose 'WebReferences'.* Another window will open and will allow you tospecify the URL of theweb service to bereferenced.Type the URL of your web service in the space provided for 'URL'. If you have created the webservice in the local machine's default web site, the URL might be like this :http://localhost/MyWebService/Service1.asmxIf you have given a different name fro the project or if you have changed the default servicename, then your URL may differ. Type the URL and press enter. It will attempt to connect withthe web service and ifsucceded it will retrieve the public web methods anddisplay. Once you getthis success screen, press the 'Add Reference' button on the screen to add the web reference toyour application. There is a spaceto specify the 'web reference name',which will have the defaultvalue 'localhost'. Leave it as it is.Now you are ready tomake calls to thewebservice. Just use the following code in yourapplication:localhost.Service1 service = newlocalhost.Service1();SUNSATThe Perfect Team134Microsoft .Netstring result = service.HelloWorld();MessageBox.Show( result );Let us analyze our code. The first line creates an instance of the Service1 class, just like anyregular class. Only thing is it usesthe namespace 'localhost'. This is same asthe 'web referencename' we specified while adding the web reference. We can use any web reference name whenwe add the reference and have to use the same name as namespace while making calls to the webservice class.In the second line, weare actually calling the web method 'HelloWorld()' and it returns theresultas a string. Note this is just like any other method call. So, your application makes the call to webservice as if it is calling any local class library calls!!The communication with web service,wrapping your calls in SOAP/XML, retrieve results fromweb service, unwrap the SOAP/XML toget the actual result etc are done behind the scene by the framework and you need not do anyadditional work to make all this work.Now you can go back to your web service, add more methods with different parameters and playaround with that. You will be ableto call methods in the webservice just like you use any localclass. Note that when you make any changes to the method name or parameters, or if you addnew methods, you will need to refresh your web reference to get access to the updated webservice 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 willcommunicate with theweb service again and retrieve thelatest web service data.How does all this work?To make calls to anyclasses, your application need to know the details ofthe class. Tosuccessfully compile your code, it has tohave information about the class, what arethe publicmethods and properties etc. But inyour sample code which calls theweb service, the actualService1 class resides in the web (in your case, in your local web server). So how does theapplication 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 createslocal proxyin your application. A local proxy is aminimal version ofthe actual class. The proxy hasjust the sufficient information about the 'real class', which isrequired for your application to successfully compile. The proxy class hasthe list of all publicweb methods in the real web service class and it knows the parameters and their data types ofeach method.The most important thing is, your application is actually using this proxy class and not the realweb service class.localhost.Service1 service = newlocalhost.Service1();string result = service.HelloWorld();MessageBox.Show( result );In the above code, localhost.Service1 is actually theproxy class (a representative of areal class),which is created automatically by VS.NET when you addedthe web reference. You are creatingan instance of the proxy classand calling the 'HelloWorld()' method of this proxy. Since theproxy class knows that it is 'only a proxy of the real class', it redirects all calls to the 'real webservice class'. The method 'HelloWorld()' in the proxy class takescare of converting the callto aSUNSATThe Perfect Team135Microsoft .Netweb request using SOAP/XML. After retrieving the result ofthe web request to the realwebservice class, this proxy class takes theresponsibility of parsing the SOAP/XML response andreturning only the result string to the calling applicationn. So theproxy class does all the dirty jobfor you.The proxy class has no implementaion ofthe functionality. It just redirects the call to thewebservice. So, if youmake any changes to theimplementation in the web service, still you will getthe 'updated result' when you make calls to web service. But, if you change the method name, orchange the parameter types in the web service and do not 'refresh the web reference', then yourapplication will compile with the old proxy. But when theproxy redirect the call tothe webservice, it will find that the parameters or method names do not match and it will fail. So, if thereis any change in the method names or parameters, you must refresh your web reference.