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

ASP .NET Web Developer s Guide - Mesbah Ahmed, Chris Garrett

.pdf
Скачиваний:
38
Добавлен:
24.05.2014
Размер:
7.32 Mб
Скачать

450 Chapter 10 • Web Services

Figure 10.7 Web References in VS.NET’s Solution Explorer Window

NOTE

A single application hosted on the Web server may access several Web Services residing on different servers. Likewise, many Web servers may access one Web Service.

.asmx Files

ASP.NET uses the .asmx file extension for defining ASP.NET Web Services.The code-behind pages are .asmx.cs and .asmx.vb for C# and VB.NET, respectively.

Migrating…

What Is the Difference between .asmx and .aspx?

In ASP, we have the .asp extension to denote an Active Server Page. When IIS sees this extension, it knows it has some extra processing to do. This is the same with ASP.NET, except that we have two new extensions, .aspx and .asmx.

Continued

www.syngress.com

Web Services • Chapter 10

451

Lets do a quick comparison:

Both file types have a template, which includes references to the primary namespaces.

.aspx pages have references to System.Drawing since their purpose is to generate a user interface.

.asmx pages have references to System.Web.Services since their purpose is to generate an interface for external programs.

You can add UI components and Data Connections to an

.aspx page.

You can add Server and Data Connections to an .asmx page.

.aspx pages usually begin with an @Page directive to designate: this is a Web Form.

.asmx pages usually begin with an @WebService directive to designate: this is a Web Service.

Using the wrong @ directive with the wrong type of file extension will generate an error.

While the client for an .aspx page is the Web browser, the client for an .asmx file is the Web server. Since they are used as programming interfaces and not directly utilized by the Web user, .asmx files should not contain any UI.To get a better understanding of how this all works, lets create an .aspx page that calls our “Hello” service.

1.In the Solutions Explorer, right-click the project name.

2.Select Add | New Item.

3.Select Web Form. Name the file helloPage.aspx.

4.While in design view, open the toolbox and drag onto the page a label and a button control from the selection of Web Forms (see Figure 10.8). While still in design mode double-click the new button.This will

generate event code in the code behind page (see Figure 10.9).

5.Right-click References in the Solution Explorer and select Add Web Reference.This is basically a graphical user interface (GUI) for the WSDL.exe command line utility.

6.When the Add Web Reference dialog opens (see Figure 10.10) click the link Web References on local server.

www.syngress.com

452 Chapter 10 • Web Services

Figure 10.8 Adding a Web Form Control to an .aspx Page

Figure 10.9 Auto-Generated Button Event Code

www.syngress.com

Web Services • Chapter 10

453

Figure 10.10 Add Web Reference Dialog Box

The dialog will pause while it searches your local machine for a list of services available.

7.When the list appears, click the name of the service that matches the name of your project, WebApplication_HelloWorld.

8.When the service loads, click the Add Reference button.This will create several new entries in your Solutions Explorer.

9.Now take a look at helloPage.aspx in HTML view.You should see code similar to the following:

<body MS_POSITIONING="GridLayout">

<form id="helloPage" method="post" runat="server"> <asp:Button id=Button1 Text="Button" runat="server" >

</asp:Button>

<asp:Label id=Label1 runat="server">Label</asp:Label> </form>

</body>

www.syngress.com

454Chapter 10 • Web Services

10.Note the name of the label control is Label1. Now open helloPage

.aspx.cs and add the following code below the label and button code.

localhost.hello test = new localhost.hello();

11. In the Button Click handler, add the following:

Label1.Text = test.HelloWorld();

12.Your code should now look like Figure 10.11.

Figure 10.11 helloPage.aspx.cs

13.Right-click helloPage.aspx and click Set as start page.

14.Press F5 to run the application.

15.When the browser loads, click the button, this will invoke our helloWorld method and assign its value to the label text. After clicking the button, your page should look like Figure 10.12.

www.syngress.com

Web Services • Chapter 10

455

Figure 10.12 HelloPage.aspx in the Browser after Clicking the Button

Developing & Deploying…

VS.NET Beta 2: Generated Template Code

When using VS.NET to develop ASP.NET pages it’s actually easier to develop using code behind than to code in the same document. When we create a new Web Form or Web Service VS.NET automatically generates a corresponding code-behind page with template code. While the template code generated may seem like more than is necessary for simple applications, the generated code makes it easy to quickly create larger event driven Web applications.

WSDL

WSDL is an XML-based language that describes Web Services. It is the composite of work done by Ariba, IBM, and Microsoft. Currently, it only supports SOAP as a messaging protocol.

The thought behind WSDL is that in future applications it will be a collection of networked-Web Services.WSDL describes what a service can do, where it lives, and how to invoke it.WSDL describes the Web Service method interfaces thoroughly enough for it to be used to create proxy methods that enable other classes to invoke its members as if they were local methods. IBM and Microsoft both have WSDL command line utilities available that do just that. IBM does it

www.syngress.com

456 Chapter 10 • Web Services

for Java, and Microsoft does it for Visual Studio.VS.NET has this ability built into the GUI. In VS.NET, we simply right-click add Web Reference and select the service we want to generate a proxy class for. Here is an example of a WSDL file for a Web Service used in Chapter 12: getCategories.wsdl.This file is auto-gener- ated by the .NET Framework.

While the auto-generated file will cover the basic functionality, it may do more or less than you intended.The auto-generated code can be simplified by removing support for asynchronous operations if you do not need to support this type of operation. Also, you could add custom SOAP headers and customize other parts of the SOAP envelope by creating your own class.

<?xml version="1.0" encoding="utf-8"?>

<definitions xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s0="http://tempuri.org/"

targetNamespace="http://tempuri.org/"

xmlns="http://schemas.xmlsoap.org/wsdl/">

<types>

<s:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://tempuri.org/">

<s:import namespace="http://www.w3.org/2001/XMLSchema" /> <s:element name="AllCat">

<s:complexType /> </s:element>

<s:element name="AllCatResponse"> <s:complexType>

<s:sequence>

<s:element minOccurs="1" maxOccurs="1" name="AllCatResult" nillable="true">

<s:complexType>

<s:sequence>

<s:element ref="s:schema" />

www.syngress.com

Web Services • Chapter 10

457

<s:any /> </s:sequence> </s:complexType>

</s:element>

</s:sequence>

</s:complexType>

</s:element>

<s:element name="DataSet" nillable="true"> <s:complexType>

<s:sequence>

<s:element ref="s:schema" /> <s:any />

</s:sequence>

</s:complexType>

</s:element>

</s:schema>

</types>

<message name="AllCatSoapIn">

<part name="parameters" element="s0:AllCat" /> </message>

<message name="AllCatSoapOut">

<part name="parameters" element="s0:AllCatResponse" /> </message>

<message name="AllCatHttpGetIn" /> <message name="AllCatHttpGetOut">

<part name="Body" element="s0:DataSet" /> </message>

<message name="AllCatHttpPostIn" /> <message name="AllCatHttpPostOut">

<part name="Body" element="s0:DataSet" /> </message>

<portType name="getCategoriesSoap"> <operation name="AllCat">

<documentation>

www.syngress.com

458 Chapter 10 • Web Services

This will return all categories in an XML String </documentation>

<input message="s0:AllCatSoapIn" /> <output message="s0:AllCatSoapOut" />

</operation>

</portType>

<portType name="getCategoriesHttpGet"> <operation name="AllCat">

<documentation>

This will return all categories in an XML String </documentation>

<input message="s0:AllCatHttpGetIn" /> <output message="s0:AllCatHttpGetOut" />

</operation>

</portType>

<portType name="getCategoriesHttpPost"> <operation name="AllCat">

<documentation>

This will return all categories in an XML String </documentation>

<input message="s0:AllCatHttpPostIn" /> <output message="s0:AllCatHttpPostOut" />

</operation>

</portType>

<binding name="getCategoriesSoap" type="s0:getCategoriesSoap"> <soap:binding style="document"

transport="http://schemas.xmlsoap.org/soap/http" /> <operation name="AllCat">

<soap:binding style="document" soapAction="http://tempuri.org/AllCat" />

<input>

<soap:body use="literal" /> </input>

www.syngress.com

 

 

 

Web Services • Chapter 10

459

<output>

 

 

 

<soap:body use="literal" />

 

 

 

</output>

 

 

 

</operation>

 

 

 

</binding>

 

 

 

<binding

name="getCategoriesHttpGet"

type="s0:getCategoriesHttpGet">

 

<http:binding verb="GET" />

 

 

 

<operation name="AllCat">

 

 

 

<http:operation location="/AllCat"

/>

 

<input>

 

 

 

<http:urlEncoded />

 

 

 

</input>

 

 

 

<output>

 

 

 

<mime:mimeXml part="Body" />

 

 

 

</output>

 

 

 

</operation>

 

 

 

</binding>

 

 

 

<binding

name="getCategoriesHttpPost"

type="s0:getCategoriesHttpPost">

 

<http:binding verb="POST" />

 

 

 

<operation name="AllCat">

 

 

 

<http:operation location="/AllCat"

/>

 

<input>

 

 

 

<mime:content type="application/x-www-form-urlencoded" />

 

</input>

 

 

 

<output>

 

 

 

<mime:mimeXml part="Body" />

 

 

 

</output>

 

 

 

</operation>

 

 

 

</binding>

 

 

 

<service

name="getCategories">

 

 

 

<port

name="getCategoriesSoap" binding="s0:getCategoriesSoap">

 

<soap:address location="http://ubid/bookSource/getCategories.asmx" />

</port>

www.syngress.com