You are on page 1of 12

Deccansoft Software Services - WebServices Agenda: 1. What is Distributed Application 2. High Level Architecture on how RMI/CORBA/DCOM works 3.

Introduction to WebService & SOAP 4. To build an WebService application and client 5. Pass by value and reference behavior 6. Working with SoapHeader Attribute 7. Asynchronous Call to the WebMethod 8. Security in WebService

Deccansoft Software Services - WebServices Distributed Applications


Replaced Monolithic applications built in the past. Business Components are now installed on one machine on the network and shared by various other applications with GUI installed on other machines on the network.

A monolithic application contains all of its code, including code to control the user interface, code to work with data, and code to enforce data validation and business rules. The major benefit to building components is that you can reuse the code to work with data and the code to enforce validation and rules. Now, multiple clients can call the same code. You have one copy of the code to maintain. You ensure every client calls the same code.

High Level Architecture (How it works on the Network)


Client Process Server Process

Client
a = 10 b = 20

Proxy

Stub
a = 10 b = 10 return = 20

Actual Object
int Add(int a, int a)

return= 20 Demo_Proxy-Stud.Dll

1. Proxy is a client side representative of the actual object (instantiated in server process). It implements the same set of
interfaces as the actual object implements.

2. When the client requests for an instance of a remote object, the underlying services (Framework) creates the actual
object in the server process and the proxy object in the client process, the client gets the reference to the local proxy object.

3. Along with the actual object a stub object is created on server. The proxy and stub are connected with each other using
a socket and communicates with each other using a common protocol.

4. Whenever the client calls the method of the proxy, the implementation in Proxy serializes all the parameters of the
method and transports them to Stub i.e. proxy marshals the request to stub.

5. On Server, Stub deserializes the request and passes the references of the objects as parameters to the actual method in
actual object. 6. Server method returns data to Stub, which serializes the data and return to proxy, proxy deserializes and return the data to the client object.

SUN (Stanford University Network RMI (Remote Method Invocation) - JRMP (Java RMI Protocol) OMG (Object Management Group) CORBA Common Object Request Broker Arch. -IIOP (Internet Interoperable Protocol MS (Microsoft) - DCOM (Distributed Component Object Model) - DEC Object RPC (Distributed Computing Environment)

Problem: Because the proxy and the stub are tightly coupled to each other there must be the same protocol for communication on both client and server. As the protocols supported by RMI, COBRA and DCOM are native to the vendors of the respective technology they lack Interoperability.

Deccansoft Software Services - WebServices

Deccansoft Software Services - WebServices About WebServices


A WebService is a business object running on a Web Server and providing its functionality to any type of application / client which supports SOAP (Simple Object Access Protocol) protocol. SOAP = XML + HTTP. SOAP is a specification by W3C. Its an architectural neutral protocol. In .Net WebServices are managed by ASP.NET Framework. They are mostly used for developing B-B (Business to Business) services/application. Any kind of application which we can develop in MS.NET can be a client to WebService. WSDL (Web Service Description Language): It is an XML based document containing complete information of the webservice including methods and their parameters, the URL etc... For every webservice developed, the corresponding WSDL document is distributed to the clients. The clients using a native utility program generates a proxy class from a WSDL document. This proxy class then can be used by client application for invoking the functionality / methods of the webservice without worrying about the existence of SOAP protocol. Steps for developing Webservice and using in a client application: Step 1: File New Web Site ASP.NET Web Service and rename Service.asmx to DemoService.asmx In DemoService.asmx: <%@ WebService Language="C#" CodeBehind="~/App_Code/DemoService.cs" Class="DemoService" %>

A webservice class autogenerated by studio has the attribute [WebService] and is also inherited from System.Web.Services.WebService class. BOTH ARE OPTIONAL REQUIREMENTS. Thus any public class linked through file with extension ASMX and deployed in the webapplication can treated as a WebService.

All the methods of a webservice class to be used by the clients on internet must be public and have WebMethod attribute.

Step 2: [WebMethod] public int Add(int a, int b) { return a + b; }

Deccansoft Software Services - WebServices


SOAP Request and SOAP Response o o o Every SOAP request and response are made up of SOAPEnvelope. SOAPEnvelope has SOAPHeader, SOAPBody, SOAPFault. SOAP always uses Http Post method and the SOAPEnvelope is submitted as MessageBody.

o A Webservice developed in .Net can be used by an Http Client also. Here the method name is submitted as the
PathInfo and the parameters are posted as a string of Name-Value pairs. http:// ServerName/ApplicationName/ ServiceName.asmx/MethodName Note: To view the WSDL document we can use the following URL http://localhost/DemoWebServices/Service.asmx?wsdl Client Application: (In New Instance of VS.NET) Step 3: File New Project Windows Application Step 4: In the Solution Explorer References Right Click Add Service Reference Advanced Add Web Reference In URL field give the location of Web Service (can copy from browser where we can see the webservice methods) Click on Add Reference. Step 5: In the same dialog: Namespace = localhost (name of domain on which the webservice is hosted is used) Click on Add Reference Note: When a WebService reference is added to the project in VS.Net it automatically creates a proxy class in the current project using a utility program called as WSDL.EXE. The same can be viewed by opening the file Reference.cs. Step 6: using WebServiceClientAppName.localhost private void btnDemo_Click(object sender, EventArgs e) { MathService ds = new DemoService(); MessageBox.Show(ds.Add(20, 50).ToString()); } Step 7: Run the Client Application Note: If a new method is added to the webservice class, right click on web reference of the webservice and select Update Service Reference to reflect the new method in the proxy class.

Deccansoft Software Services - WebServices


Pass by Value and Pass by Reference behavior for both Value Types and Reference Types
In Demoservice add the following WebMethod Step 8: Add the following to the WebService class [WebMethod] public void Foo(int a, ref int b) { a++; b++; } Step 9: To reflect the changes made to the webservice in the client application: Right Click on NameSpace of WebService (Localhost) and Select Update Web Reference. Step 10: Add a button to the form and handle its button click event private void btnFoo_Click(object sender, EventArgs e) { int m = 10; int n = 10; DemoService ds = new DemoService(); ds.Foo(m, ref n); MessageBox.Show(m + " " + n); } Step 11: Run the client application and observe the o/p. Step 12: Add the following class to the WebService Project: public class CA { public int m1; } Step 13: Add the following method to the webservice class [WebMethod] public void Foo2( CA a1, ref CA a2) { a1.m1++; a2.m1++; } Step 14: private void btnFoo2_Click(object sender, EventArgs e) { CA ma1 = new CA();

Deccansoft Software Services - WebServices


CA ma2 = new CA(); DemoService ds = new DemoService(); ds.Foo2(ma1, ref ma2); MessageBox.Show(ma1.m1 + " " + ma2.m1); } Step 15: Run the client application and observe the o/p. Notes: View the SOAP Envelop for Foo and Foo2 to check what Parameters are serialized between Client and Server in both SOAP request and SOAP response.

o If the parameter of the web method is pass by value then the reference type of argument is serialized and transported
only from client to server. Hence changes made to the paremeter in the web method on server are not reflected in the client.

o If the parameter of the web method is pass by reference then the reference type of argument is serialized and
transported in both the direction i.e from client to server and server to client. Hence changes made the to parameter in the web method on server are also reflected in the client. Note: In context of a WebMethod the behavior of the parameters is same for both class (reference type) and structure (value type). The behavior is like value type not reference type when we compare with calling local methods.

Deccansoft Software Services - WebServices


Working with SoapHeader
Because Soap depends on Http protocol the webservices are stateless objects. SoapHeader can be used for passing certain additional data to a WebMethod. Once a SoapHeader is associated with the proxy on the client, the proxy automatically includes the data along every method called by it on webservice. Step1: Add a new class to the webservice project (in App_Code folder) and inherit from SoapHeader public class UserInfoHeader : SoapHeader { public string UserName, password; public int Count; } Step2: Within Webservice class declare a public variable of custom SoapHeader type public UserInfoHeader ui; Step3: Attach a SoapHeader to a Web Method using SoapHeader attribute. SoapHeaderDirection.InOut means SoapHeader is exchanged in both the directions. Step4: In the method we can use the reference variable [WebMethod] [SoapHeader("ui", Direction = SoapHeaderDirection.InOut)] public int Add(int a, int b) { if (ui. UserName != ui.password) throw new ApplicationException("Invalid username or password"); ui.Count++; return a + b; } [WebMethod] [SoapHeader("ui", Direction = SoapHeaderDirection.InOut)] public int Sub(int a, int b) { if (ui. UserName != ui.password) throw new ApplicationException("Invalid username or password"); ui.Count++; return a - b; } Note: View/Open the webservice in the web browser and see how the SOAP Envelope of a given method with SOAP Header has changed. If a member of SOAP Header class is used for managing state between calls to the multiple webmethods by the client, then the direction of the SOAP Header must be given as InOut. This is because only then the SoapHeader data modified on server will be returned to the proxy and proxy only then for subsequent calls to either same or different webmethods will include the modified SoapHeader. The client has to pass the instance of SoapHeader once to the proxy and then the proxy transparently exchanges the SoapHeader with the webservice for every call to the webmethod .

Deccansoft Software Services - WebServices


Step5: In Client - Code for attaching SoapHeader to the proxy private void btnSoapHeader_Click(object sender, EventArgs e) { DemoService ds = new DemoService(); UserInfoHeader ui = new UserInfoHeader(); ui.username = "a"; ui.password = "a"; ds.UserInfoHeaderValue = ui;// attaching header to proxy MessageBox.Show(ds.Add(10, 20).ToString()); ui = ds.UserInfoHeaderValue; MessageBox.Show(ui.Count.ToString()); MessageBox.Show(ds.Sub(10, 20).ToString()); ui = ds.UserInfoHeaderValue; //To get the latest value of SoapHeader from proxy MessageBox.Show(ui.Count.ToString()); }

Deccansoft Software Services - WebServices Asynchronous Call to the WebMethod


If the webmethod is called asynchronously the client application never remains unresponsive while proxy has called and is waiting for the return value of actual webservice webmethod. For every WebMethod X in a webservice the following are generated along with the proxy class.

1. The method X 2. The method XAsync: To begin Asynchronous call. 3. The event XCompleted: Event to handle when the Asynchronously called method returns 4. The delegate XCompletedEventHandler: Delegate for the above event. 5. The class XCompletedEventArgs: This is parameter of the methods and can be used to retrieve return value.
private void btnSoapHeader_Click(object sender, EventArgs e) { DemoService ds = new DemoService(); ds.AddAsync(1, 2); //Calling the method Asynchronously ds.AddCompleted += new AddCompletedEventHandler(ds_AddCompleted); MessageBox.Show("Continue"); } void ds_AddCompleted(object sender, AddCompletedEventArgs e) { MessageBox.Show(e.Result.ToString()); } Note: For Asynchronous call to a web method, we dont have to make any changes to the webservice or its web method because calling methods asynchronously is a feature of MS.NET and has no support in SOAP Protocol or WebService. //This method is called by proxy when the actual method returns.

10

Deccansoft Software Services - WebServices Security in WebService


WebServices supports only Windows Authentication. Forms Authentication is not supported because they dont have User Interface for creating a login form. Add a web.config to the DemoService and include the following code <system.web> <authentication mode="Windows" /> <authorization> <deny users="?"/> </authorization> </system.web> In IIS for the webservice virtual directory either set Integrated Windows / Basic Authentication. [WebMethod] public string SayHello() { return "Hello" +" "+ User.Identity.Name; } In ClientApplication add the following code private void Security_Click(object sender, EventArgs e) { DemoService ds = new DemoService(); //ds.Credentials = System.Net.CredentialCache.DefaultCredentials; //Code to explicitly set the username and password in the request. System.Net.CredentialCache cc = new System.Net.CredentialCache(); System.Net.NetworkCredential nc = new System.Net.NetworkCredential(txtUsername.Text, txtPassword.Text); cc.Add(new Uri(ds.Url), "basic", nc); cc.Add(new Uri(ds.Url), "NTLM", nc); //if NTLM doesnt work then give Negotiate ds.Credentials = cc; MessageBox.Show(ds. SayHello()); } System.Net.CredentialCache.DefaultCredentials includes the identity of the client using which we have logged on to the client machine.

11

Deccansoft Software Services - WebServices


Limitation of WebService:

1. Because WebService use SOAP protocol which is based on HTTP, as HTTP is stateless the business objects developed
as webservice are also stateless. For every WebMethod called, a new instance of the webservice class is created initializing all its data members to their default values. 2. Web Services do not support Callback implementation i.e. a web service cannot call the method of client object.

12

You might also like