Escolar Documentos
Profissional Documentos
Cultura Documentos
• Learn how to expose your business logic with XML Web services
• Learn the differences between XML Web services and DCOM
• Learn how to deploy XML Web services
• Learn how to call XML Web services, synchronously and asynchronously
Assumptions
The following should be true for you to get the most out of this document:
• You are familiar with coding in Microsoft® Visual Basic® 6.0
• You have a basic understanding of Microsoft .NET
• You have access to a Microsoft SQL Server™ database
• You have access to Visual Basic 6.0, Microsoft ASP.NET, DCOM Server, Microsoft
Visual Basic .NET and Microsoft Visual Studio® .NET
Contents
Introduction
DCOM versus Web Services
DCOM Introduction
XML Web Services Introduction
ASP.NET Introduction
Converting DCOM to an XML Web Service
The DCOM Server
The DCOM Client
The XML Web Service
The .NET Windows Application Client
Summary
About the Author
Introduction
The Microsoft .NET Framework provides a rich alternative to the Distributed COM (DCOM)
protocol in the form of XML Web services provided by ASP.NET. XML Web services allow
businesses to distribute their application services to a broader audience than is otherwise possible
with DCOM. It accomplishes this by providing a loosely coupled messaging infrastructure,
encapsulating and exposing business application logic using industry standard protocols and data
formats such as Extensible Markup Language (XML), Hyper-Text Markup Language (HTTP)
and SOAP (formerly Simple Object Access Protocol).
By incorporating these industry standards within XML Web services, businesses can expose
their application logic to global clients and partners that were previously inaccessible. XML Web
services provide a loosely coupled messaging infrastructure independent of vendor specific
messaging implementations. Businesses can now easily integrate their existing applications with
those residing on heterogeneous platforms, regardless of the programming model used. XML
Web services allow businesses to deliver their application logic over the World Wide Web
(WWW) to any type of client, on any platform, as long as they support the same industry
standards set forth by, and submitted to the W3C.
This document examines the advantages of using XML Web services over DCOM and
demonstrates how to implement an XML Web service and consume it with a .NET client
application.
DCOM versus Web Services
DCOM enabled software developers to create applications that span multiple machine, network,
and location boundaries, allowing scalability and ease-of-distribution across multiple tiers.
Although additional complexity was introduced into the development effort, most of the benefits
provided by DCOM (e.g., location independence, security and scalability) were realized to
varying degrees. After the release of MTS and COM+, DCOM became easier to implement and
became the standard protocol employed among most Microsoft solution providers. Later,
Microsoft Application Center was released and provided load balancing and fault tolerance to
COM+ components.
As the Internet evolves, the nature and scope of distributed applications must change to meet the
underlying business needs. Businesses must integrate their applications with those that reside on
heterogeneous platforms, and those that are built and deployed with varying programming
models. Additionally, businesses need to communicate and expose their services to global clients
and partners.
To address these needs, XML Web services were introduced as part of ASP.NET, which is part
of the .NET Framework. Web services are based on open Internet standards, such as HTTP,
XML, and SOAP. Using these open standards, Web services deliver application functionality
across the Web to any type of client, on any platform.
Although XML Web services is the enabling technology, it is Visual Studio .NET that
encapsulates its ease of use for developers. Visual Studio .NET provides a robust environment
that allows the easy creation, deployment, and maintainability of applications developed using
XML Web services.
DCOM Introduction
DCOM is based on the original Distributed Computing Environment (DCE) standard Remote
Procedure Call (RPC) infrastructure and was created as an extension of Component Object
Model (COM) to allow the creation of server objects on remote machines. In order for COM to
create remote objects, the COM libraries need to know the network name of the server. Once the
server name and CLSID (a globally unique identifier representing a COM Class within the
server) are known, the Service Control Manager (SCM) on the client machine connects to the
SCM on the server machine and requests creation of the remote machine's server object. Because
DCOM is an extension of COM, it relies on the registry and COM libraries to supply the type
library information of the object to create on the remote server machine. The remote server name
is either configured in the registry or passed as an explicit parameter to a CoCreateInstanceEx
call (in Visual Basic, this would be a CreateObject call).
Copy
Sub StartIE()
Dim strProgID As String
Dim oIE As InternetExplorer
StrProgID = "InternetExplorer.Application.1"
' Clean up
Set oRst = Nothing
End Function
' Validate
If Len(Trim$(CustomerID)) < 1 Then _
Err.Raise 9999, "Add", _
"You must enter a Customer ID to add."
' Clean up
oConn.Close
Set oConn = Nothing
End Sub
' Validate
If Len(Trim$(CustomerID)) < 1 Then _
Err.Raise 9999, "Update", _
"You must select a Customer ID to update."
' Clean up
oConn.Close
Set oConn = Nothing
End Sub
' Validate
If Len(Trim$(CustomerID)) < 1 Then _
Err.Raise 9999, "Delete", _
"You must select a Customer ID to delete."
' Clean up
oConn.Close
Set oConn = Nothing
End Sub
Next, compile the project into the DemoVBServer.DLL, open the COM+ Explorer (see Figure
2), and create a new COM+ Server Application called DemoVBServer set to run under a specific
security context (user id). Then add the DemoVBServer.Customers class as a new component
within the DemoVBServer COM+ Server Application.
Delete_Err:
MsgBox Err.Description
End Sub
Exit Sub
Update_Err:
MsgBox Err.Description
End Sub
' Cleanup
Set moCust = Nothing
End Sub
strCustomerID = lstCust.List(lstCust.ListIndex)
lstCustClick_Exit:
' Clean up
Set oRst = Nothing
Exit Sub
lstCustClick_Err:
MsgBox Err.Description
Resume lstCustClick_Exit
End Sub
Figure 4. Web services project created in Solution Explorer with Show All Files selected
Open the project Properties dialog box and set the following properties:
• Option Explicit = ON
• Option Strict = On
• Option Compare = Text
• Configuration = Release
Next, rename Service1.asmx file to Customers.asmx. This file represents the final Web service
that will be available. When the file is renamed, Visual Studio .NET also renames the source
code file for the service to Customers.asmx.vb and updates the Customer.asmx file with the
correct references. Right-click this file and select View Code.
At the top of the Customers.asmx.vb file add the following:
Copy
Option Compare Text
Option Explicit ON
Option Strict On
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Security.Principal
The Imports statement allows access to objects in the .NET Framework hierarchy without fully
qualifying their namespace. For example, instead of specifying
Copy
Dim custDataSet as System.Data.DataSet
Use the following declaration instead:
Copy
Dim custDataSet as DataSet
The next series of steps involve the creation of the SOAP Header class and the Customers class.
The Customers class will contain all the exposed methods of the Web service. Both classes will
be created within the Customers.asmx.vb file. The first class to create is a SOAP Header class
called AuthHeader. This is used to accept a username, password, and an IsAuthenticated
parameter from the client. These are later optionally validated within the GetCustomer method of
the Customers class using the ValidateUser method. Here is the source code for this class.
Copy
Public Class AuthHeader
Inherits SoapHeader
' These hold the values retrieved from the SOAP Header
' sent by the client.
Private UsernameEx As String = Nothing
Private PasswordEx As String = Nothing
Private Authenticated As Boolean = False
<WebMethod(EnableSession:=False, _
Description:="Returns all customers " & _
"or a customer by customer ID"), _
SoapHeaderAttribute("AuthHeaderMemberVariable", _
Direction:=SoapHeaderDirection.In, _
Required:=True)> _
Public Function GetCustomer( _
ByVal customerID As String) As DataSet
There is one method call signature, GetCustomer, that helps understand the functionality exposed
by the Web service. Here is the method signature of GetCustomer.
Copy
<WebMethod(EnableSession:=False, _
Description:= _
"Returns all customers or a customer by customer ID"), _
SoapHeader("AuthHeaderMemberVariable", _
Direction:=SoapHeaderDirection.In, Required:=True)> _
Public Function GetCustomer( _
ByVal customerID As String) As DataSet
The only difference between this signature and the one in the previous DCOM server example is
that this one is decorated with Attributes. Attributes applied to a method signature are enclosed
in "<" and ">" when using Visual Basic .NET, and enclosed in "[" and "]" when using C#. At
design time, these describe the functionality that will be associated with the component. Because
they represent classes themselves, the component inherits the base class of the Attribute.
The first Attribute, WebMethod represents the WebMethodAttribute class and identifies the
method as callable from a Web service. It is also used to enable the session state and append a
description to the method. This description will be represented in the WSDL information
produced for the Web service. This can be viewed by navigating to a Web service and appending
?WSDL to the URL like so,
http://MachineName/WebServiceVirtualDirectory/ServiceName.asmx?WSDL. Finally, the
SoapHeader Attribute represents the SoapHeaderAttribute class. This Attribute passes the public
variable instance of the AuthHeader class. This variable will be populated with the SOAP
Header information sent by the client and become available within the method call.
Once the code is completed in the project, execute the build process to create the
DemoWebSrv.dll. After the build is complete, view the service description page by opening
http://localhost/DemoWebSrv/Customers.asmx in a browser (see Figure 5). This page is auto-
generated by ASP.NET and will include links to each method call defined by the WebMethod
Attribute. Each link is directed to a page that allows the invocation of the method using the
HTTP-GET protocol. Additionally, the Service Description link displays the WSDL service
description information for the Web service. This can later be used with the WSDL.EXE tool to
generate client proxy classes.
Imports System
Imports System.Security.Principal
Imports System.Windows.Forms
Imports System.Web.Services.Protocols
Add controls to the form so that it looks like the form in Figure 6.
' Populate the list box and retrieve the current user
Try
' set the cursor to the hourglass
Me.Cursor = Cursors.WaitCursor
Try
' Set the cursor
Me.Cursor = Cursors.WaitCursor
Try
' set the cursor to the hourglass
Me.Cursor = Cursors.WaitCursor
' Cast the sender object into a control
ctl = CType(eventSender, Control)
Case "cmdUpdate"
' Retrive the customer id and update.
If Not (lstCust.SelectedItem Is Nothing) Then
CustomerID = lstCust.SelectedItem.ToString
CustomerID = lstCust.SelectedItem.ToString
' These are all the call backs used for the async calls.
Public Sub UpdateCustCallBack(ByVal ar As IAsyncResult)
Try
' set the cursor
Me.Cursor = Cursors.WaitCursor
m_CustWebSrv.EndUpdate(ar)
Try
' set the cursor
Me.Cursor = Cursors.WaitCursor
m_CustWebSrv.EndDelete(ar)
Try
' Set the cursor
Me.Cursor = Cursors.WaitCursor
Summary
XML Web services provide a flexible and robust infrastructure for the creation and consumption
of Web services. Web services can be created easily and can be extended with the .NET
Framework and Visual Studio .NET. Most developers will no longer have to learn the intricacies
of underlying infrastructure because .NET does it all. And nothing can be easier than creating
client applications that consume Web services using Visual Studio .NET. The client-side proxies
are generated automatically, hiding the details of the marshalling calls from the developer.
Accessing a Web service method is as easy as it was accessing an object method in COM, except
without the pain of registration, type libraries, and binary compatibility issues.
The .NET Framework and XML Web services technologies will prove to be empowering tools
for your development efforts going forward.
About the Author
Marty Wasznicky is a Senior Systems Engineer with Microsoft Corporation in the Southern
California district, focusing on Enterprise Application Integration, Business to Business and E-
Commerce. He has more than ten years of experience architecting and implementing solutions in
the IT industry in both corporate and consulting capacities. He is a Microsoft Certified Solutions
Developer, Microsoft Certified Systems Engineer, Microsoft Certified Database Administrator
and Certified Novell Engineer. He has authored numerous articles in the Visual Basic
Programmer's Journal and the Access/VB/SQL Advisor magazines.