ASP.NET and Web Services

The ASP.NET framework simplifies development of web services. All the low-level work, such as packaging and unpackaging data in XML format and utilizing HTTP protocol to transport the web messages between distributed components, are done by the framework. This allows the developers to focus on the application logic.

The .NET Framework uses asmx as the default file extension for web services, as opposed to aspx for Web Forms and ascx for web controls.

The WebService Directive

All asmx files start with the @WebService directive that instructs ASP.NET on how to compile the code, as well as the main class name. The WebService directive has the following attributes:

Language

Specifies the language in which the code was written. This instructs the ASP.NET framework to use the appropriate compiler to build your web service. Use vb for Visual Basic and c# for C#. As other languages emerge, obviously you can specify other languages.

Class

Specifies the main class, which exposes web methods. The ASP.NET framework instantiates this class in order to serve the web methods to the clients.

Codebehind

Specifies the source file for your code, which allows for complete code/ASP separation.

You can easily create a simple web service similar to the following asmx file:

<%@ WebService Language="VB" Class="MyClass" %>
Public Class MyClass
  Public Function <WebMethod(  )> Add(a as integer, b as integer) as 
    integer
    Return a + b
  End function
End class

If you prefer to separate your code completely from any ASP.NET elements, you could have the code for your web service saved in a separate file and specify the Codebehind attribute of the @WebService directive to point to the code file:

<%@ WebService Language="VB" Codebehind="MyClass.vb" Class="MyClass" %>

The WebMethod Attribute

Public methods of any classes can be tagged with the WebMethod attribute to be made accessible from the Web. The syntax for tagging attributes to methods is different for each .NET language. For example, in C# the tag takes the following form:

[WebMethod(attribute="value" attribute="value" ...)]
    public returnType FunctionName(paramsList)

In VB, angle brackets are used instead of square brackets, and the tag location is also different:

Public Function <WebMethod(attribute="value" attribute="value" ...)>
  FunctionName(paramsList) as returnType

Public Sub <WebMethod(attribute="value" attribute="value" ...)>
  SubName(paramsList)

Using Web Services

If you are using Visual Studio.NET,[45] you can choose ProjectAdd Web Reference and then type in the URL where the web service resides. For our purpose, we’ll point to the web service we created in the last chapter, PubsWS. The URL to this web service on our server is http://localhost/PubsWS/PubsWS.asmx. After adding the web reference, you can access the proxy object to the web service you are calling via the type servername.proxyObjectName. For your case, it is localhost.PubsWS.

The following code excerpt demonstrates how to use the web service through the proxy. We create an instance of the proxy object and then ask it to relay the message to the real web service to get the list of authors. The result will be streamed back in XML format, which is reconstructed into a DataSet object. We then bind DataGrid1, which is just a DataGrid object that we have on the Web Form, to the default view of the first table of the DataSet. Finally, we ask for the actual binding to take place. The resulting page is the grid populated with rows from the Authors table of the Pubs sample database.

localhost.PubsWS ws = new localhost.PubsWS(  );
DataSet ds = ws.GetAuthors(  );
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind(  );

Instead of using Visual Studio.NET to locate and automatically generate the proxy class, you can also use the information from the previous chapter to generate the source for the proxy class yourself. You can then include this source or compile the source into a DLL and add the DLL to the project as a reference. In any case, the end result is the same. Here is an example that links against the proxy we created in the previous chapter and fills a grid with data:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>

<!-- Link to the proxy generated by wsdl.exe -->
<%@ Assembly Src="PubsWS.cs" %> 

<html>
  <head>
    <title>SOAP Client</title>
  </head>
  <body>

  <!-- Make the SOAP call and fill the data grid. -->
  <%
    PubsWS ws = new PubsWS(  );
    DataSet ds = ws.GetAuthors(  );
    dg.DataSource = ds.Tables[0].DefaultView;
    dg.DataBind(  );
  %>

  <!-- Create a data grid. -->
  <asp:DataGrid id="dg" runat="server"/> 

  </body>
</html>


[45] We use VisualStudio.NET Beta 1 in this book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.2.218