Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

CREATE A WEB SERVICE

he next evolution of distributed programming, Web TServices, allows for your applications to provide

component-based services over the Internet. That is, you can call a .NET component from one machine on the Internet to another. Web Services are made available through standards like Simple Object Access Protocol (SOAP), eXtensible Markup Language (XML), and HyperText Transport Protocol (HTTP). This mechanism allows for calls to be made over known communication ports, like port 80, the standard port for HTTP. For Microsoft, Web Services are considered the basic building blocks for distributed applications.

Because Microsoft has a SOAP Toolkit that allows remote procedure calls on COM+ components over HTTP, you do

not need .NET or VS .NET for building Web services, but having VS .NET and .NET makes life much easier when you are creating or using a Web Service.

VS .NET has a project type of ASP.NET Web Service to assist in creating Web services. Creating a Web service involves the use of a few new file types, which you may not find familiar, including the *.asmx and *.vsdisco. When you first start creating Web services, you need to be primarily concerned with the *.asmx file. The *.asmx file is where you will place your Web methods. The syntax is similar to how you create methods in classes. The only major difference is the use of the attribute WebMethod for methods that need to be exposed by the Web service and the class must be derived from system.Web.Services.WebService.

CREATE A WEB SERVICE

ASP.Net Web Service

Type a name for the Web service.

ˇ Click to select http://localhost for the Location.

Á Click OK.

Double-click Web service file created in the Solution Explorer.

° Scroll down to the

[WebMethod] and select the text for the sample Web service.

· Delete the comment lines.

Right-click Web service file in the Solution Explorer.

Click Build and Browse.

CREATING AND DEPLOYING DISTRIBUTED APPLICATIONS 14

The Web service that you created in the sample task was a simple "Hello World" for a Web service. If you want to go a step further, you can test out the below sample Web method. You will need to create a wellformed XML document,

Favorites1.xml, in the same directory as the Web service file.

TYPE THIS:

using System.IO; [WebMethod]

public string GetFavoritesList(int UserID) { string sServerPath = Server.MapPath("");

// Here you could make a database call to get XML. string sFilePath = sServerPath+ "\\" + "Favorites1.xml"; string sList = GetXMLAsString(sFilePath);

return sList;

}

private string GetXMLAsString(string XMLDocumentPath) { FileStream fsFavorites = new FileStream

(XMLDocumentPath,FileMode.Open,FileAccess.Read);

StreamReader srFavorites = new StreamReader(fsFavorites); return srFavorites.ReadToEnd();

}

RESULT:

The xml string in favorites.xml is returned from the

WebMethod.

The

is loaded

± Click

C#

USING A WEB SERVICE

y using a Web service in your client application or Bserver application, you can utilize resources across the

Internet and open up new possibilities of truly distributed architectures.

When you build an ASP.NET Web Service, it automatically supports clients using the SOAP, HTTP-GET, and HTTP-POST protocols to invoke Web Service methods. HTTP-GET and HTTP-POST send information via named value pairs, but do not allow for complex data types to be passed. However, SOAP, or Simple Object Access Protocol, allows for more complex data types to be passed due to SOAP’s support of XML and XSD schemas.

Consuming a Web service is well supported in VS .NET, which includes a wizard-based approach to discovery and configuration of the proxy you need to make the SOAP call. The interface provided for discovering the Web service enables you to browse to the URL of the service. When you access the service, you can test the services from the interface, view WSDL (Web Services Description Language), contract, and view any documentation that exists. After you have a Web reference, you can import the namespace and then use it like a local component.

USING A WEB SERVICE

References

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Type a name.

 

In the Solution Explorer,

° Click Add Web Reference.

 

 

 

 

 

 

 

 

 

 

ˇ Click to select a location

right-click the class file.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

for the console application.

 

 

 

 

 

 

 

 

 

Á Click OK.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CREATING AND DEPLOYING DISTRIBUTED APPLICATIONS 14

Heavily investing into the future of Web Services, one of Microsoft’s current initiatives, HailStorm, addresses common programming needs such as personal profile information and contacts. Below is a list of some of the HailStorm Services.

 

HAILSTORM SERVICES

 

 

 

 

 

 

 

myAddress

Electronic and geographic address for an identity

 

 

 

 

 

 

myProfile

Name, nickname, special dates, picture

 

 

 

 

 

 

myContacts

Electronic relationships/address book

 

 

 

 

 

 

myLocation

Electronic and geographical location and rendezvous

 

 

 

 

 

 

myNotifications

Notification subscription, management, and routing

 

 

 

 

 

 

myInbox

E-mail and voice mail, including existing mail systems

 

 

 

 

 

 

myCalendar

Time and task management

 

 

 

 

 

 

myDocuments

Raw document storage

 

 

 

 

 

 

myApplicationSettings

Application settings

 

 

 

 

 

 

myFavoriteWebSites

Favorite URLs and other Web identifiers

 

 

 

 

 

 

myWallet

Receipts, payment instruments, coupons, and other transaction records

 

 

 

 

 

 

myDevices

Device settings and capabilities

 

 

 

 

 

 

myServices

Services provided for an identity

 

 

 

 

 

 

myUsage

Usage report for above services

 

 

 

 

 

 

 

 

 

· Type the URL to the Web Service.

Note: See page 270 for more information on creating a Web Service.

Click OK.

Open the class file.

± Add an alias to the Web service namespace.

¡ Rename the namespace to HelloConsoleApplication.

Create a new variable of type Service1.

£ Write the result of the call to the WebMethod to the console.

¢ Set a debug stop.

Press F5.

The output to the

console appears.

273

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