ASP.NET and Web Services Part 2 - The .asmx File
(Page 3 of 8 )
Because the implementation of a web service is encapsulated within a class, it is important to define an end point for the web service. The .asmx file serves as the end point for the web service. When a client calls the .asmx file, the ASP.NET runtime processes the file.
Usually, each .asmx page has a language directive at the top of the page, as displayed here:
<%@ WebService Language="C#" Class=
IFCEBrokerage.SecuritiesExchange" %>
The first time the web service is called, the ASP.NET runtime uses the Language directive to compile the code first to MSIL and subsequently to the language specified by the directive. In this case, the directive specifies the C# .NET programming language. This means the IFCEBrokerage class code shares the CLR runtime services
and data types defined in the CTS with all other .NET programming languages.
NOTE
The Class attribute bears the fully qualified name of the class that implements the web service. Because the default language in the machine.config file is set to “vb,” the IFCEBrokerageFirm.asmx language should be set to C# .NET. Here is the machine.config file segment:
<compilation debug="false"
explicit="true" defaultLanguage="vb">
It is also wise to place the web service’s implementation in its own assembly. By convention, the assembly is placed within the web application’s bin directory because this directory is placed within the search path. The SecuritiesExchange web service client can access this web service only through HTTP for the simple reason that ASP.NET supports only this transmission protocol. However, the web service supports three kinds of protocol bindings:
All ASP.NET web services support SOAP because the SOAP body content is strongly typed and is XML based through the XSD Schema.
Web Services Documentation ASP.NET’s runtime includes services such as documentation. This is a major benefit because the CLR uses System.Reflection to generate two kinds of documentation:
- Documentation utilized by clients so they can interact with web services
- Documents referenced by people
HTML-based documents are accessible by entering the URL in a browser. Both the WebMethod and its companion attributeWebService expose the Description property.
Listing 7 demonstrates how to include documentation in the IFCEBrokerage.asmx file.
Listing 7 Adding the Description attribute
using System
;
using
System.Web;
using System.Web.Services;
namespace
IFCEBrokerage
{
[WebService(Description ="This demonstrates how to
use the Description attribute")]
public class SecuritiesExchange :
System.Web.Services.WebService
{
[WebMethod]
public double StockQuote(string
symbol)
{
double
quotePrice= 0;
switch(symbol)
{
case
"JANS":
quotePrice =
29.95;
break;
case
"MSFT":
quotePrice =
49.97;
break;
case
"ORCL":
quotePrice =
37.72;
break;
}
return quotePrice;
}
}
}
Remember: This is part one of chapter 7 .NET & J2EE Interoperability, by Dwight Peltzer (McGraw-Hill/Osborne, ISBN 0-07-223054-1, 2004). If you like what you see, feel free to click on the following link to get your own copy! Buy this book now.
|
Next: Automatically Generated Documentation >>
More ASP.NET Articles
More By McGraw-Hill/Osborne