ASP.NET and Web Services, Part 1 - Creating a Web Service
(Page 3 of 9 )
Microsoft designates ASP.NET as the technology for building web services. Both business logic and presentation layer are bundled together within ASP.NET. The developer begins the web service process by applying attributes that inform the Common Language Runtime (CLR) back to treat a defined class and its methods as a web service. (Refer to Chapter 5 for a complete discussion about the CLR and its functionality.) Web service business logic is stored in an .asmx file. The code can dwell either within the .asmx file or in a precompiled class. The developer applies the following directive to the file containing the class name and namespace in which the class for the web service resides. It looks like this:
<%@ WebService Language = "VB" Class ="IFCE" %>
Then, the developer declares a method that includes the number of parameters and its data types, as shown here:
<WEBMETHOD()><WebMethod()>Public Function ConvertCurrency
(ByVal x As Double, ByVal y as Double) As Double
//write the code to implement the method
End Function
The code is compiled to Microsoft Intermediate Language (MSIL) and to its own specific assembly. Typically, the assembly should be placed in a bin file so others can reference it. Creating a web service class looks like this:
Imports System
.Web.Services
Public Class IFCE
Inherits WebService
'declare the methods here exposed by the Web Service
End Class
XML-based web services use the namespace mechanism for defining an end point for the web service, the namespace to whom a client forwards requests. Here is the syntax:
<WebService (Namespace="http://www.IFCE.com/Webservices/") _
Public Class IFCE: WebService {}
ASP.NET uses reflection inspect metadata attributes. Attributes facilitate applying WebMethod attribute to declare methods as previously demonstrated. By examining this process, it is clear that the client requests a server-side provider to expose its services. The provider is mandated to issue the client a contract (aWSDL file) that lists the services it exposes. This information informs the client how to communicate with the service provider and utilize its services.
Therefore, it is possible to add to our definition of a web service by describing it as a discrete piece of business logic written in Java, managed C++ .NET, C# .NET, or some other programming language. Depending on the language, a function or method is typically decorated with a web method attribute, thereby designating it as eligible to initiate a call to a web service provider. This process informs the CLR that this call is to be treated as a valid web service invocation.
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: Defining a Service-Oriented Architecture (SOA) >>
More ASP.NET Articles
More By McGraw-Hill/Osborne