XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access...

20
XML Web Service

Transcript of XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access...

Page 1: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

XML Web Service

Page 2: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

Overview of XML Web Service

ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network

Web services are built on open standards – such as HTTP, XML, and SOAP

Page 3: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

Creating a Simple XML Web Service

<%@ WebService Class="TemperatureService" %>Imports SystemImports System.Web.ServicesPublic Class TemperatureService : Inherits WebService <WebMethod()> Public Function ToCelsius( TF As Double ) As Double Return ( 5/9 ) * ( TF - 32 ) End Function <WebMethod()> Public Function ToFahrenheit( TC As Double ) As Double Return ( 9/5 ) * Tc + 32 End FunctionEnd Class

TemperatureService.asmx

Page 4: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

WebService & WebMethod Attributes

<WebMethod(Description:=“Converts Fahrenheit to Celsius”)>Public Function toCelsius(TF As Double) As Double …End Function

<WebService(Description:=“Temperature Service”, NameSpace:=“http://127.0.0.1/Hello”)> Public Class TemperatureService …End Class

Page 5: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

Invoking an XML Web Service with HTTP-Get

<a href=“localhost/service/temperatureService.asmx/ToCelsius?TF=32”>Convert</a>

Example:

Page 6: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

Invoking an XML Web Service with HTTP-Post

<form method="post" action="/Services/TemperatureService.asmx/ToCelsius"><input name="TF" value="32"><input type="submit" value="Convert!"></form>

Example:

Page 7: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

To create a Web Service project 1.On the File menu, point to New, and then click Project. 2.In the New Project dialog box, select either the Visual Basic

Projects or the Visual C# Projects folder. 3.Click the ASP.NET Web Service icon. 4.Change the name of the project to what you want it to be. 5.If you are not developing the Web Service on your localhost Web server, enter the address of the Web server on which you will develop the Web Service. 6.Click OK to create the project.

Create a Web Service Project

Page 8: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

When using Visual Studio.NET to create Web Services in managed code, you use a standard deployment model: you compile your project and then you deploy the resulting files to a production server. The project DLL file contains the Web Services code-behind class file (.asmx.vb or .asmx.cs) along with all other class files included in your project, but not the .asmx file itself. You then deploy this single project .dll file to the production server without any source code. When the Web Service receives a request, the project .dll file is loaded and executed.

Create a Web Service Project

Page 9: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

Accessing an XML Web Service Through a Proxy Class

Step 1: Use the Wsdl.exe tool to generate the source code file for the proxy class

Example:

wsdl.exe /l:vb http://www.yoursite.com/services/test.asmx?wsdl

test.vb

Page 10: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

Accessing an XML Web Service Through a Proxy Class

Step 2: Compile the source code file for the proxy class

Example:

vbc /t:library /r:System.dll,System.Web.Services.dll,System.Xml.dll text.vb

test.dll

Page 11: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

Accessing an XML Web Service Through a Proxy Class

Step 3: Copy the compiled proxy class into the ASP.Net application’s /bin directory

Page 12: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

Using an XML Web Service Proxy Class

<script runat=“Server”>

Sub Button_Click(s as Object, e as EventArgs) Dim objTemp As New TemperatureService lblTest.Text = objTemp.ToCelsius(txtTest.Text)End Sub

Page 13: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

To Add a Web Reference to a Project

1. In Solution Explorer, select a project that supports adding Web references.

2. On the Project menu, choose Add Web Reference. 3. In the Add Web Reference dialog box, type the URL

for the Web Service in the Address text box, and then choose the Arrow Icon.

4. Verify that the items in the Available References box are the items you want to reference in your project, and then choose Add Reference.

5. In Solution Explorer, expand the Web References folder to note the namespace for the Web reference classes that are available to the items in your project.

Page 14: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

When accessing Web Services in managed code, a proxy class and the .NET Framework handle all of the infrastructure coding. Accessing a Web Service follows these basic steps:

•Locate the Web Service you want to access. •Create a proxy class for the Web Service by adding a Web reference to your project. •Reference the proxy class in the client code by including its namespace. •Create an instance of the Web Service proxy class in the client code. •Access the Web Service using the methods of the proxy.

Accessing Web Services in Managed Code

Page 15: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

To simplify the coding model, applications written in managed code use a Web reference to locally represent each Web Service. You add a Web reference to your project using the Add Web Reference dialog. This dialog box supports browsing Web addresses for a Web Service.

Locating a Web Service and Adding a Web Reference

Page 16: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

Reference the Proxy Class

The generated proxy class has its own namespace associated with it, and you must add the namespace to your client application before you can create an instance of that class.

Page 17: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

Create an Instance of the Proxy Class

Before you can call any of the methods of the proxy class, you must first create an instance of that class. This process does not differ at all from creating an instance of any other class.

Page 18: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

Access the Web Service Using the Proxy

To access a Web Service method, your client application invokes either the corresponding synchronous method or asynchronous methods of the proxy object. These methods in turn do the necessary work to remote the call over the wire to call the desired Web Service method. By default, the proxy class uses SOAP to access the Web Service method, as SOAP supports the richest set of data types of the three supported protocols (HTTP-GET, HTTP-POST, and HTTP-SOAP).

Page 19: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

To Add a New Web Service to an Existing Web Project

1.In Solution Explorer, select the project to which you want to add a Web Service. 2.On the Project menu, click Add Web Service. Alternatively, right-click the project name, point to Add and then click Add Web Service. 3.The Add New Item dialog appears. By default, the dialog box selects the ASP.NET Web Service template and supplies a name for the new Web Service in the Name box. 4.Click Open to add the Web Service to your project.

Page 20: XML Web Service. Overview of XML Web Service ASP.NET XML Web services enable you to remotely access the properties and methods of classes across a network.

To Add an Existing Web Service to an Existing Web Project

1.In Solution Explorer, select the project to which you want to

add an existing Web Service. 2.On the Project menu, select Add Existing Item. Alternatively, right-click the project name, point to Add and then click Add Existing Item. The Add Existing Item dialog box appears. 3.In the Files of type list, click Web Files. 4.Select the .asmx file for the Web Service you want to add. 5.Click Open to add the Web Service to your project.