440 Chapter 9 • Debugging ASP.NET
Q:I noticed during tracing that the Session ID for my application changes when I refresh my page or when I do a postback.Why is this happening?
A:For performance reasons, the .NET Framework does not maintain state between the Web server and the Web browser automatically, hence the Session ID is always different between submissions. However, when the Session object is used or when the Session_OnStart() event is added to the global.asax file, the Session ID would be maintained between postbacks.
Chapter 10
Web Services
Solutions in this chapter:
■Understanding Web Services
■Using XML in Web Services
■An Overview of the System.Web.Services Namespace
■Type Marshalling
■Using DataSets
;Summary
;Solutions Fast Track
;Frequently Asked Questions
442 Chapter 10 • Web Services
Introduction
Web Services provide a new level of interaction to ASP.NET applications.The ability to access and use a remote Web service to perform a function within an ASP.NET Web application enables programmers to quickly deliver a more sophisticated app in less time.The programmer no longer has to create and maintain all functions of the application. Reusability is also greatly enhanced by creating multiple Web services that perform functions in multiple applications, thus freeing up time and resources to work on other aspects of specific projects. See Figure 10.1, which shows a graphical representation of this process.
Figure 10.1 Where Do Web Services Fit In?
|
Host Web Services |
|
|
Data |
|
|
Internet and |
|
|
Database |
|
|
Servers |
|
Host Web Pages |
Host Web Pages |
Host Web Pages |
Internet Servers IIS |
Internet Servers IIS |
Internet Servers IIS |
|
Internet Users |
|
Workstations |
Workstations |
Workstations |
|
Workstations |
Workstations |
www.syngress.com |
|
|
Web Services • Chapter 10 |
443 |
Web Services function primarily through XML in order to pass information back and forth through the Hypertext Transfer Protocol (HTTP).Web Services are a vital part of what the .NET Framework offers to programmers. XML-based data transfer is realized, enabling primitive types, enumerations, and even classes to be passed through Web Services to the application performing the request.This brings a whole new level of reusability to an application. XML is the backbone from which the whole Framework is built.The user interface (UI) can be created by applying Extensible Stylesheet Language Transformations (XSLTs) or by loading the data into DataSets and binding to Web Controls. Having XML as the intermediary enables new avenues of client design.
Understanding Web Services
Web Services are objects and methods that can be invoked from any client over HTTP.Web Services are built on the Simple Object Access Protocol (SOAP). Unlike the Distributed Component Object Model (DCOM) and Common Object Request Broker Architecture (CORBA), SOAP enables messaging over HTTP on port 80 (for most Web servers) and uses a standard means of describing data. SOAP makes it possible to send data and structure easily over the Web.Web Services capitalizes on this protocol to implement object and method messaging.Web Services are easy to create in VS.NET. Here is an ASP.NET Hello World class in C#:
public class hello
{
public string HelloWorld()
{
return "Hello World";
}
}
}
This class describes a hello object that has one method, HelloWorld.When called, this method will return data of type string.To convert this to a Web Service method, we simply have to add one line of code:
public class hello
{
[WebMethod]
public string HelloWorld()
444 Chapter 10 • Web Services
{
return "Hello World";
}
}
}
A little bit more code is involved to make this a method of a Web Service. This is the code that VS.NET auto-generates when we create a new .asmx page, along with our Hello World method:
using |
System; |
using |
System.Collections; |
using |
System.ComponentModel; |
using |
System.Data; |
using |
System.Diagnostics; |
using |
System.Web; |
using |
System.Web.Services; |
namespace Hello
{
public class Hello : System.Web.Services.WebService
{
public Hello()
{
InitializeComponent();
}
private void InitializeComponent()
{
}
protected override void Dispose( bool disposing )
{
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
Web Services • Chapter 10 |
445 |
}
}
}
You can quickly create this class in VS.NET by creating and opening a C# Web Application project or Web Service project and adding a new WebService page.
If you prefer, similar code could be written to create a VB.NET Service:
Imports System.Web.Services
Public Class Service1
Inherits System.Web.Services.WebService
<WebMethod()> Public Function HelloWorld() As String
HelloWorld = "Hello World"
End Function
End Class
Configuring & Implementing…
Setting the Start Page
When testing a Web service in a project that contains other .aspx or
.asmx files, be sure to set the file you are debugging/testing to be the Start page, before running. To do this, right-click the filename in the
Solution Explorer and select Set as start page.
To run this sample in VS.NET, simply press F5. It will take a few moments to build and compile.When that phase is complete, you should see the Hello service screen shown in Figure 10.2.
The top line on the screen states that the operations listed below it are supported.This is followed by a bulleted list of links to each of the Web methods that belong to the Web service. In our case, we created only one Web method, HelloWorld. If you click the link HelloWorld, you will be taken to that service’s description page (see Figure 10.3).
446 Chapter 10 • Web Services
Figure 10.2 Hello Service
Figure 10.3 HelloWorld Service Description Page
Web Services • Chapter 10 |
447 |
To test our Hello Web Services HelloWorld Web method, simply click the Invoke button and our method will be called. Recalling our method returns the string “hello world”; the result is returned in an XML wrapper (see Figure 10.4).
Figure 10.4 Results from Invoking the HelloWorld Web Method
Note that the XML node reflects the datatype of the method’s return value, string.This XML message is received and converted to the string “Hello World”. This means that any variable (of type string) in our code can be assigned to the result of our Web method.
Configuring & Implementing…
Building and Compiling
If you have experience programming in C/C++ or Java, you will be familiar with the building and compiling steps. If you are a Web Developer who hasn’t really played with a compiled language before, these steps will be new to you. Think of it as the phase in which the compiler gets all your code together and checks it for unassigned variables, variable type mismatches, and other syntactic errors. In this phase, it also converts your code into the Common Language Runtimes (CLR) Intermediate Language (IL), and then into machine language. This will allow the code to run faster and more efficiently than uncompiled script. After this phase completes, the code is run in the Browser. So, testing Web page output may seem to take longer in the .NET environment.
448 Chapter 10 • Web Services
Communication between Servers
The concept of sending messages between servers or remotely calling functions is not new.Technologies such as DCOM and CORBA are well-known proprietary protocols that have been in use for years.What is new is the use of a standard protocol to transfer messages over HTTP, that protocol is SOAP. SOAP makes it possible for applications written in different languages running on different platforms to make remote procedure calls (RPC) effectively, even through firewalls. DCOM doesn’t use port 80, which is reserved for HTTP traffic; this causes DCOM calls to be blocked by firewalls. SOAP calls use port 80, which makes it possible to call procedures that exist behind firewalls. Figure 10.5 shows a high level overview of how Web Services can be used, both for customer interactions with a company from multiple client types as well as for internal company data gathering and reporting between all company servers, including legacy systems.
Figure 10.5 Overview: Where Do Web Services Fit In?
Customer Web Services
Branch kiosk can allow customers to pull or retrieve account information.
Corporate Website can allow customers to pull or retrieve account information.
Investment Brokerage House
Headquarters
Data
Corporate / |
Corporate |
Customer Web |
Reporting Web |
Services |
Services |
Corporate Web Services
Branch offices can push reporting data to corporate servers.
Corporate can pull branch reporting data.
Branch Office
Data
Server
Soap / HTTP
Using SOAP over HTTP enables servers running different operating
systems to communicate seamlessly over the Internet.
SOAP // HTTP
Internet
Customers
Branch Office
Data
Server
Soap / HTTP
Using SOAP over HTTP enables applications written in different
languages to communicate seamlessly over the Internet.
Web Services • Chapter 10 |
449 |
In ASP.NET,Web Services and their methods are defined in pages with the
.asmx extension.When we create Web Services, the .NET Framework generates a Web Services Description Language (WSDL) file on the server hosting the Service; this WSDL file describes the Web Service interface. On the Web server that hosts our .aspx pages,VS.NET generates a WSDL proxy when we click Add Web reference in the Solutions Explorer and select the server and Service (see Figure 10.6).
Figure 10.6 Overview: Where WSDL and WSDL Proxies Fit into the Internet User Page Request Process
Web Service
Server Hosts .asmx
Pages
Data
Server
WSDL
SOAP // HTTP
Internet
WSDL Proxy
|
Server |
Web Server Hosts |
|
.aspx Pages |
|
|
Customers
Scenario
Web user makes an online purchase:
•One method call could verify and process the credit card with a Web Service supplied by the credit card organization.
•Another method could contact a Web service supplied by the shipping company to calculate shipping charges.
•Another Web service from within
the organization could remove the item from active inventory and flag it for shipping.
Figure 10.7 shows a Web reference for “localhost” and the WSDL proxies for each Web Service that exists on that server.