Visual Basic and System.Web.Services


The SOAP Toolkit provides a number of wizards to navigate most of the obstacle course required to set up a Web Service, but the .NET Framework class library provides the abstract classes. The System.Web.Services namespace provides four classes and three other namespaces that enable programmatic exposure of methods to the Web.

System.Web.Services Namespace

The System.Web.Services namespace includes the following component classes:

  • WebService

  • WebMethodAttribute

  • WebServiceAttribute

  • WebServicesBindingAttribute

The WebService class is the base class from which all the ASP.NET services are derived, and it includes access to the public properties for Application, Context, Server, Session, Site, and User. ASP programmers will recognize these objects from the ASP namespace. Web Services can access the IIS object model from the WebService class, including application-level variables:

  Imports System.Web Imports System.Web.Services Imports System.Web.Services.Protocols <WebService(Namespace:="http://www.lipperweb.com/namespace")> _ <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ Public Class Util     Inherits System.Web.Services.WebService    <WebMethod(Description:="Application Hit Counter", EnableSession:=False)> _     Public Function HitCounter() As String        Dim HitCounter As Integer        If (Application("HitCounter") Is DBNull.Value) Then           Application("HitCounter") = 1        Else           Application("HitCounter") = Application("HitCounter") + 1        End If        HitCounter = Application("HitCounter")        Return HitCounter    End Function End Class 

WebService is an optional base class, used only if access to ASP.NET objects is desired. The WebMethodAttribute class, however, is a necessity if the class needs to be available over the Web.

The WebServiceAttribute class is similar to the WebMethodAttribute class in that it enables the addition of the description string to an entire class, rather than method by method. We recommend adding it before the previous class declaration:

 <WebService(Description:="Common Server Variables")> _ Public Class ServerVariables     Inherits System.Web.Services.WebService

Instead of using WSDL in the contract to describe these services, the System.Web.Services namespace provides programmatic access to these properties. IIS Service Discovery uses these descriptions when queried. This way, you have removed the necessity to struggle with myriad protocols surrounding Service Contract Language and SOAP.

System.Web.Services.Description Namespace

The System.Web.Services.Description namespace provides a host of classes that provide total management of the WSDL descriptions for your Web Service. This object manages every element in the WSDL schema as a class property.

For example, the preceding discussion on the benefits of WSDL description mentioned the benefits of being able to query a Web Service about its methods and parameters. The System.Web.Services.Description namespace provides methods for the discovery of methods and parameters, gathering the information from the service contract and providing it to the object model in Visual Basic code.

When working on the HTTP GET protocol (as opposed to SOAP, for instance), simply pass in the required sEmail parameter through the use of a querystring. You can find details about this in the Web Service’s WSDL description. In the successive <wsdl:message> sections, you find all parameter info for all three protocols, including HTTP GET (if enabled via the web.config file):

  <wsdl:message name="IsValidEmailHttpGetIn">    <wsdl:part name="sEmail" type="s:string" /> </wsdl:message> <wsdl:message name="IsValidEmailHttpGetOut">    <wsdl:part name="Body" element="tns:boolean" /> </wsdl:message> 

Invoking this Web Service using HTTP GET, use the following construct:

  • http://localserver/Validate.asmx?sEmail=evjen@yahoo.com

Note that HTTP GET is disabled by default because it is deemed a security risk. If you wish to enable HTTP GET for your XML Web Services, then configure it for this in the web.config file of your Web Service solution, as shown here:

  <configuration>    <system.web>       <webServices>          <protocols>             <add name="HttpGet"/>          </protocols>       </webServices>    </system.web> </configuration> 

System.Web.Services.Discovery Namespace

The System.Web.Services.Discovery namespace provides access to all of the wonderful features of the .disco files on a dynamic basis. Because Microsoft is currently trying to integrate Web Services as a remoting protocol and is not pushing the public service side as much, you don’t see the use of .disco files as often in the Microsoft side of things. Your business partner might be using them, though, so this namespace proves useful. For instance, you can access the DiscoveryDocument using the Discovery class:

  Imports System.Web.Services.Discovery ReadOnly Property DiscoveryDocument(strURL As String) As DiscoveryDocument    Get       DiscoveryDocument = DiscoveryClientProtocol.Discover(strURL)    End Get End Property 

Like the System.Web.Services.Description namespace, the System.Web.Services.Discovery namespace provides many tools to build a .disco document on-the-fly.

System.Web.Services.Protocols Namespace

All of the wire service problems solved with HTTP and SOAP are handled in the System.Web.Services.Protocols namespace. When handling references to classes also referenced in other Web Service namespaces, the System.Web.Services.Protocols namespace proves to be a handy tool. The objects referenced by the System.Web.Services.Protocols namespace include the following (among others):

  • Cookies per RFC 2019

  • HTML forms

  • HTTP request and response

  • MIME

  • Server

  • SOAP, including SoapException, the only error-handling mechanism

  • URIs and URLs

  • XML

The System.Web.Services.Protocols namespace is particularly handy for managing the connection type by a client. A consumer of a Web Service can use the HTTP GET or HTTP POST protocol to call a service, as well as the HTTP SOAP protocol. Microsoft’s .NET initiative focuses on SOAP as the ultimate means of connecting disparate data sources. The System.Web.Services.Protocols.SoapDocumentMethodAttribute class enables developers to set special attributes of a public method for when a client calls it using SOAP:

  Imports System.Web Imports System.Web.Services Imports System.Web.Services.Protocols <WebService(Namespace:="http://www.lipperweb.com/namespace")> _ <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ Public Class Util     Inherits System.Web.Services.WebService   <SoapDocumentMethod(Action:="http://MySoapMethod.org/Sample", _    RequestNamespace:="http://MyNamespace.org/Request", _    RequestElementName:="GetUserNameRequest", _    ResponseNamespace:="http://MyNamespace.org/Response", _    ResponseElementName:="GetUserNameResponse")> _    WebMethod(Description:="Obtains the User Name")> _   Public Function GetUserName()     '...   End Function End Class 




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net