Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

C# ПІДРУЧНИКИ / c# / Hungry Minds - ASP.NET Bible VB.NET & C#

.pdf
Скачиваний:
128
Добавлен:
12.02.2016
Размер:
7.64 Mб
Скачать

 

Table 27-1: Disco tool command-line options

 

 

 

 

 

 

 

 

 

Option

 

Description

 

 

 

 

 

 

 

 

 

the tool.

 

 

 

 

 

 

The Disco tool copies and generates the following files into the folder where you executed the Disco tool or the folder specified by the /out option. Let's look at an example using the Disco tool to locate the Web service description files for our CTemp Web service.

First, let's assume for a moment that we only know the name of the Web server that is hosting our CTemp service, but would like to discover the complete URL to it. We can do this by typing the following at the command prompt:

disco /nosave http://localhost

This command will initiate the discovery process against the local Web server, searching for all discovery documents and related files that are referenced in these discovery documents. Note that we are not saving any of the results yet (using the /nosave option).

When dynamic discovery is enabled at the root of the Web server (the default), the Disco tool will search hierarchically through all Web server folders. Running the previous command against my Web server produces the results shown in Figure 27-1.

Figure 27-1: Using the Disco tool to find Web services

Notice that the Disco tool has listed all the discovery documents it found on the Web server as well as a reference to a single WSDL document for our CTemp Web service. Now that we know the complete URL to the CTemp Web service, let's use the Disco tool one more time to discover information specific to the CTemp Web service. This time, we will save the results to a temporary folder, using the following command:

disco /out=c:\temp http://localhost/ctemp/ctemp.vsdisco

This will initiate the discovery process against the discovery document of the CTemp Web service located on your local Web server. Once again, executing this command on my Web server produces the results shown in Figure 27-2.

Figure 27-2: Using the Disco tool to discover information about the CTemp Web Service Note that the Disco tool has saved three files to your local computer. These files are summarized in Table 27-2.

Table 27-2: Files created by the Disco tool

File

Ctemp.wsdl

Description

The WSDL document for the CTemp Web service

Ctemp.disco

 

The

 

 

discovery

 

 

document

 

 

for the

 

 

CTemp Web

 

 

service

 

 

 

Results.discomap

 

A discovery

 

 

document

 

 

that

 

 

contains

 

 

references

 

 

to the local

 

 

copies of

 

 

the

 

 

Ctemp.wsdl

 

 

and

 

 

Ctemp.disco

 

 

files

In actual practice, you would save the results of the discovery process to the project folder where you were building the client application that consumes the Web service just discovered.

Armed with the information and files copied to our system by the Disco tool, we could use the wsdl tool to generate a proxy class for the CTemp Web service, which could be used to consume the service in a client application. We will discuss this process later in this chapter in the section "Creating a proxy class with the WSDL tool."

Finding Web Services with UDDI

While the Disco tool is an effective means of locating Web service descriptions, you must know the URL to either a specific target server where the Web service is deployed

or a specific Web service virtual directory on a Web server. In any case, you won't be able to find any Web services using Disco without knowing a URL.

As mentioned previously, UDDI is a more generalized search tool for locating Web services. Using UDDI, you can specify search terms that enable you to pinpoint Web services you wish to use based on company, industry, service type, and many other classifications. Using these more generalized search terms usually results in the listing of specific URLs to which you can locate information about specific Web services, including the WSDL document that you are ultimately searching for.

In this section, we will explore how to find Web services using the interactive UDDI Web site. This Web site provides search forms for locating Web services (and Web service descriptions). To illustrate the search process, we will search for Web services that are published by Microsoft.

Let's start by browsing to the UDDI Web site at http://www.uddi.org. After you get there, click the Find link at the top of the page. This displays the Find page. On this page, choose the Microsoft UDDI node from the list box and click GO. This brings you to the Microsoft UDDI node search page, as shown in Figure 27-3.

Figure 27-3: The Microsoft UDDI Search Page

Notice that the search page provides category -based browsing that enables you to locate Web services based on various company classifications, including ISO 3166 Geographic Taxonomy and Standard Industrial Classification, among others.

In the middle of the page is the Advanced Search form. From here, you can enter text in the Search For text box to perform a custom search of various fields within the database. You can locate Web services in this manner by searching any one of the following field types:

§Business Name: Use this field to search within registered business names. For example, you could search for services provided by IBM.

§Business Location: Use this field to search by geographic location. For example, you could search for companies that provide services within the St. Louis area.

§Service Type By Name: Use this field to search services submitted by a company by the service name.

§Business Identifier: Identifiers are pieces of data that are unique to an individual business, such as the standard D-U-N-S™ Number. There are

several standard, as well as custom, identifiers that a company can register.

§Discovery URL: Provides a location where details about a particular entity can be found. An example would be the URL to the company Web site or a URL for a particular Web service.

§GeoWeb Taxonomy: Provides a standard, hierarchical classification of geographic locations by which to search.

§NAICS Codes: The North American Industry Classification System provides a standard, hierarchical classification of businesses based on the products or services they provide.

§SIC Codes: The Standard Industrial Classification provides a standard, hierarchical classification of businesses based on industry.

§UNSPSC Codes: Universal Standard Products and Services Codes provides a standard, hierarchical classification of businesses based on the products and services they provide.

§ISO 3166 Geographic Taxonomy: Provides a standard, hierarchical classification of businesses based on geography; similar to the GeoWeb Taxonomy.

§RealNames Keyword: RealNames is a keyword-like name, similar to AOL keywords, that are registered to specific companies.

Let's do a simple search by business name and take a look at what Microsoft has provided in terms of Web service registrations.

Choose Business Name from the drop-down list, enter "Microsoft" in the Search For text box, and then submit the form. The search facility will look for any businesses with the name Microsoft in the Business Name field and return a list of hits. In this example, you should see a single listing. Click the Microsoft Corporation link and you will see a page similar to Figure 27-4.

Figure 27-4: UDDI business registration information for Microsoft

Scroll the page until you see the list of services offered. Here you will find an entry named Web Services for Smart Searching. Click this link and the browser will display a Service Detail page. Scroll this page until you find the Bindings section. The browser window should look similar to Figure 27-5.

While this book was being written, the UDDI nodes at Microsoft and IBM were still very immature. The search capabilities on the Microsoft node had several bugs, and both sites had few businesses registered. Because this is a new technology, it appears that some patience will be required before these registries become truly useful to the average developer.

Figure 27-5: Microsoft Smart Searching Web Service bindings detail

The interesting information displayed here is supplied by the Access Point link. Note that the URL points to the entry point of two categorized Web service interfaces:

§A Vocabulary service

§A Best Bets service

Each service shares a common URL. Go ahead and click one of the entry point links. By now you know that pointing your browser to an ASMX file will return the ASP.NET Web Service description page. From this page, you can access the WSDL contract, which can be saved to your local hard drive and then interrogated by the wsdl tool to create a Web service proxy class. You will learn more about this process shortly.

Congratulations! You have just located your first Web service from the UDDI business registry! Of course, myriad ways exist to search the registry. Which one you choose depends on what you are looking for and how much information you have related to the Web service.

Caution

If you are not using Visual Studio to build or consume Web services, you will want to become familiar with the facilities provided by the UDDI business registry nodes. Over time, these sites will hopefully provide one-stop shopping for locating high-quality Web services that you can incorporate into your custom solutions.

Although a simple and effective solution for finding Web services, you may encounter some cases in which you cannot use Visual Studio's Web Reference feature, such as when the Web service is not accessible from the machine on which you are using Visual Studio. Under these circumstances, you can use the Web Service Description Language Tool (wsdl.exe) to generate a Web Service client proxy class. We will cover this tool in the next section.

Web Service Interrogation and Proxy Classes

Web service interrogation is the process of examining a Web service description to determine how to interact with the Web service as a consumer. As you have learned, a Web service communicates with consumers using messages encoded in XML and encapsulated in SOAP. You have also seen that these messages can be transported over one of several protocols (such as HTTP, SMTP, and so on). Lastly, the Web service

description defines the message formats and exchange patterns for the particular methods, arguments, and return values that the Web service supports.

If you were required to code directly to these technologies, consuming Web services would be a time-consuming (and probably painful) process. What's worse, you would be spending time writing what is essentially plumbing code to consume a Web service, rather than focusing (and rightly so) on the actual task you were attempting to accomplish.

Fortunately, you do not have to worry about creating and formatting SOAP messages, or understanding how to exchange messages with various transport protocols. All of this plumbing is hidden from you via the Web service proxy class. Instead, you simply instantiate an instance of the proxy class and make calls to its methods in a completely object-oriented fashion.

Recall that the .NET Framework provides the wsdl tool for parsing Web service description files. But, in addition to interrogating these files, the wsdl tool is able to generate a proxy class, which provides an object-oriented interface for calling methods on the Web service and returning the results to the caller.

Visual Studio automatically creates Web service proxy classes for you when you use the Add Web Reference dialog box to create a reference to a Web service. If, however, you are unable to use Visual Studio's Web Reference feature, you can create the proxy class manually using the wsdl tool. Let's take a look at the tool and how it can be used to create Web service proxy classes that can be used by Web service consumers.

Creating a proxy class with the WSDL tool

The .NET Framework provides a tool named wsdl.exe to parse Web service descriptions and generate proxy classes, which can be used by a consumer to call methods on a Web service. The wsdl tool is capable of generating proxy classes given any one of the following types of files as input:

§WSDL files

§XSD (XML Schema Definition) files

§DISCO files

§DISCOMAP files

Note that these files are all outputs of .NET's Disco tool. By default, the wsdl tool is located in the same place as the Disco tool:

Program Files\Microsoft.NET\FrameworkSDK\Bin

The wsdl tool is a console application, so you will need to start it from within a command window just like the Disco tool discussed in the last section.

Tip

You may want to add the path to the .NET Framework Bin folder to

 

your system's PATH environment variable so that the tools can be

 

located without typing the path to them on the command line.

The general format of the wsdl command line is as follows:

wsdl [options] {URL | Path}

where URL specifies the HTTP address of a target Web server that you wish to search for any of the supported file types previously listed (excluding DISCOMAP files), and Path specifies the local file path to any of the supported file types listed.

The wsdl tool supports the command line options listed in Table 27-3.

Table 27-3: WSDL tool command-line options

Option

/appsettingurlkey:key

or

/urlkey:key

Description

Specifies the configuration key to use to read the default value for the URL property when generating code. This enables you to obtain the URL of the

Table 27-3: WSDL tool command-line options

Option

/appsettingbaseurl:baseurl

or

/baseurl:baseurl

/d[omain]:domain

/l[anguage]:language

/n[amespace]:namespace

/nologo

Description

Web service from the ASP.NET configuration file, rather than having it hard-coded in the proxy class.

Specifies the base URL to use in conjunction with the

/appsettingurlkey option. The tool calculates the URL fragment by converting the relative URL from the baseurl argument to the URL in the WSDL document. You must specify the

/appsettingurlkey option with this option.

Specifies the domain name to use when connecting to a server that requires authentication.

Specifies the language to use for the generated proxy class. You can specify CS (C#; default), VB (Visual Basic), or JS (JScript) as the language argument. You can also specify the fully qualified name of a class that implements the System.CodeDom.Comp iler. CodeDomProvider Class. The default language is C#.

Specifies the namespace for the generated proxy or template. The default namespace is the global namespace. As an example, setting the namespace to TempConverter would permit us to reference our CTemp proxy class as TempConverter.CTemp.

Suppresses the Microsoft startup banner display.

Table 27-3: WSDL tool command-line options

Option

/o[ut]:filename

/p[assword]:password

/protocol:protocol

/proxy:URL

/proxydomain:domain

or

/pd:domain

/proxypassword:password

or

/pp:password

/proxyusername:username

or

/pu:username

/server

Description

Specifies the file in which to save the generated proxy code. The tool derives the default filename from the Web Service name. The tool saves generated datasets in different files.

Specifies the password to use when connecting to a server that requires authentication.

Specifies the protocol to implement. You can specify SOAP (default), HttpGet, HttpPost, or a custom protocol specified in the configuration file.

Specifies the URL of the proxy server to use for HTTP requests. The default is to use the system proxy setting.

Specifies the domain to use when connecting to a proxy server that requires authentication.

Specifies the password to use when connecting to a proxy server that requires authentication.

Specifies the username to use when connecting to a proxy server that requires authentication.

Generates an abstract class for a normal (server-side) Web Service based on the contracts. The default is to generate client proxy classes. This option will probably rarely be used, but is useful in cases where you have defined a standard interface for a Web service, but may wish to create several separate implementations.

 

Table 27-3: WSDL tool command-line options

 

 

 

 

 

 

 

 

 

Option

 

Description

 

 

 

 

 

 

 

/u[sername]:username

 

Specifies the username

 

 

 

 

 

 

 

 

to use when connecting

 

 

 

 

to a server that requires

 

 

 

 

authentication.

 

 

 

 

 

 

 

/?

 

Displays command

 

 

 

 

 

 

 

 

syntax and options for

 

 

 

 

the tool.

 

 

 

 

 

 

The following example illustrates the use of the wsdl tool to generate a proxy class for our CTemp Web service:

VB.NET:

wsdl /l:vb /out:CTempProxy.vb http://localhost/ctemp/ ctemp.asmx?wsdl

C#:

wsdl /l:cs /out:CTempProxy.cs http://localhost/ctemp/ ctemp.asmx?wsdl

This example creates a proxy class in the specified language syntax and saves it to a file named CTempProxy.vb (or CTempProxy.cs, depending on which language you choose). The proxy is generated from the WSDL provided by the URL specified on the command line.

The output of the wsdl tool in the command window should look similar to Figure 27-6.

Figure 27-6: Results of executing the wsdl tool

The following proxy class source code generated by the wsdl tool is for VB.NET: '----------------------------------------------

----------------------

'<autogenerated>

'This code was generated by a tool.

'Runtime Version: 1.0.2914.16

'

'Changes to this file may cause incorrect behavior and will be lost if

'the code is regenerated.

'</autogenerated>

'-----------------------------------------------

---------------------

Option Strict Off

Option Explicit On

Imports System

Imports System.Diagnostics

Imports System.Web.Services

Imports System.Web.Services.Protocols

Imports System.Xml.Serialization

'

'This source code was auto-generated by wsdl, Version=1.0.2914.16.

'

<System.Web.Services.WebServiceBindingAttribute

(Name:="TempConverterSoap", [Namespace]:="http://mydomain.com/ws/ctemp")> _ Public Class TempConverter

Inherits

System.Web.Services.Protocols.SoapHttpClientProtocol

<System.Diagnostics.DebuggerStepThroughAttribute()> _ Public Sub New()

MyBase.New

Me.Url = "http://jdc7200cte/services/ctemp/ctemp.asmx"

End Sub

<System.Diagnostics.DebuggerStepThroughAttribute(), _ System.Web.Services.Protocols.SoapDocumentMethodAttribute

("http://mydomain.com/ws/ctemp/CTemp", RequestNamespace:=

"http://mydomain.com/ws/ctemp", ResponseNamespace:=

"http://mydomain.com/ws/ctemp", Use:=System.Web.Services.Description.

SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.

SoapParameterStyle.Wrapped)> _

Public Function CTemp(ByVal Temperature As Decimal, ByVal FromUnits As String, ByVal ToUnits As String) As Decimal

Dim results() As Object = Me.Invoke("CTemp",

New Object() {Temperature, FromUnits, ToUnits})

Return CType(results(0),Decimal)

End Function

<System.Diagnostics.DebuggerStepThroughAttribute()> _ Public Function BeginCTemp(ByVal Temperature As Decimal,

ByVal FromUnits As String, ByVal ToUnits As String, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult

Соседние файлы в папке c#