Building Microsoft ASP.NET Applications for Mobile Devices, Second Edition [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Building Microsoft ASP.NET Applications for Mobile Devices, Second Edition [Electronic resources] - نسخه متنی

Andy Wigley; Peter Roxburgh

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
تنظیمات قلم


اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
افزودن یادداشت جدید

Consuming an XML Web Service

You can test any XML Web service by simply calling it from a Web browser and accessing its Web methods. Just enter the URL of the Web service—for example, http://localhost/MyWebService/Service1.asmx. For Web Services hosted on an IIS server that has the .NET Framework installed, what is returned is a page that describes the service and allows you to invoke it from the page, as shown in Figure 15-2. However, in real life, you'll probably integrate the XML Web service into a program you're writing. This section describes how to consume an XML Web service programmatically.

Figure 15-2: Directly calling an XML Web service from a Web browser

Consuming XML Web Services Using Visual Studio .NET

You typically consume an XML Web service by calling methods of a local proxy class that in turn accesses the Web methods within the remote XML Web service. In a Visual Studio .NET project, you generate the proxy class by using the Add Web Reference feature.

Create a new mobile Web application, and name it ConsumeMyFirstWebService. This application will contain the mobile Web Forms page that will consume the XML Web service you created earlier. Create a mobile Web Forms page with a single Label control. The Label control should have a blank Text property. The process of consuming an XML Web service has two stages. The first stage entails adding a Web reference to the XML Web service you want to consume to your Visual Studio .NET project:

In the Solution Explorer, right-click on the project to select it.

Select Add Web Reference from the menu.

When the Add Web Reference dialog box appears, click the Web Reference On Local Web Server link (Visual Studio .NET 2002) or the Web Services On The Local Machine link (Visual Studio .NET 2003). Figure 15-3 shows the Add Web Reference dialog box from Visual Studio .NET 2003.

Figure 15-3: Add Web Reference dialog box

Wait a few seconds for the list of available XML Web services to be displayed. Select the XML Web service you want to consume.

Defining XML Web Service Behavior," later in this chapter. Click the Add Reference button.

You've now added a Web reference to the XML Web service you want to consume. Visual Studio .NET has automatically downloaded the WSDL document that describes the Web service (see the sidebar WSDL Documents for more information) and used that information to create the proxy class you use to access the XML Web service, and this proxy class is now part of your application.

WSDL Documents

Every XML Web service publishes a Web Service Description Language (WSDL) document written in XML alongside the actual Web service itself. The document is known as a service description and is accessible by clients who want to use your XML Web service. The purpose of the document is to describe how an XML Web service acts. For example, a WSDL document might specify which methods an XML Web service provides, what parameters these methods accept, and what data types they return. In other words, this document tells a user what to expect from HTTP or SOAP method calls. You can think of a service description as a contract between an XML Web service and its potential users that says, "Here I am, this is what I do, and this is how I do it."

You can view the service description of any publicly accessible XML Web service that supports HTTP. To do so, open a Web browser, type the URL of the remote XML Web service, and append ?WSDL to the URL to call the service description. Figure 15-4 shows the service description of a "Hello World" application like the one you've just written.

Figure 15-4: Internet Explorer displaying a service description

The proxy class has been generated using the namespace ConsumeMyFirstWebService.localhost. To keep your code concise when you use the methods of this class, you should import this namespace into your code-behind module. Here's the syntax:

using ConsumeMyFirstWebService.localhost;


When you add a Web reference to your project using Visual Studio .NET (the 2002 release), the generated proxy class is placed into a namespace in the form projectNamespace.remoteHostName. For example, if you add a Web reference to an XML Web service on your local machine, Visual Studio .NET defines the generated proxy in the projectNamespace.localhost namespace. The namespace Visual Studio .NET selects is not a meaningful name for the XML Web service, but you can change this by renaming the Web service reference using Solution Explorer.

In Visual Studio .NET 2003, you can set the namespace for the proxy from the Add Web Reference dialog box. Simply enter the required name in the Web Reference Name box in this dialog box and then click the Add Reference button.

Through the proxy class you can use any of the methods of the XML Web service as though it were a local object. In this example, you can access the HelloWorld method of the XML Web service. You can use the result to set the value of the Label control's Text property in the mobile Web Forms page you created earlier by adding this syntax to the code-behind module:

private void Form1_Load(Object sender, System.EventArgs e)
// Create a new instance of the Web Service proxy class.
Service1 service1 = new Service1();
// Call the HelloWorld method.
String msg = service1.HelloWorld();
// Assign the result to the Text property of the Label.
Label1.Text = msg;


If you're unsure of a class name or method name, you can find it by viewing the WSDL file for the XML Web service. To do so, you either access the service description by using a browser (as described previously) or view the WSDL document within Visual Studio .NET. You can access the document through Solution Explorer. If you don't see a file with a .wsdl extension, click the Show All Files icon to ensure that all the project's files appear.

When you execute the code, the Form1_Load method creates a new instance of the Service1 class, which is the proxy object. You then can call the HelloWorld method on this object. Next the proxy object makes a request to the remote XML Web service, which returns an XML response. The proxy object parses the response and returns the data value to the caller of the HelloWorld method. The consumer application then sets the Text property of the Label control to the value fetched from the XML Web service. Figure 15-5 shows the output of this code, as viewed in a the Nokia emulator.

Figure 15-5: Output of the Web service consumption code

Web Service Discovery

You might be wondering how Visual Studio .NET knows which XML Web services are available for you to consume. You might also be wondering how you can find those XML Web services. The answer to both questions is Web service discovery.

Web service discovery is the process of locating the URLs of XML Web services on a remote server. This process accesses discovery (.disco) files, which are files that contain links to resources that describe an XML Web service. When you add a Web reference to your project, Visual Studio .NET performs a Web service discovery. However, you can perform a Web service discovery from the command line by using the Disco.exe tool, which performs a search and saves the results to your local machine. For more information about this tool and Web service discovery, refer to the .NET Framework SDK documentation.

/ 145