Professional Documents
Culture Documents
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
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.
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.
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.
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.
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.
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
11
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