The Components of a Web Service Project


Visual Studio and the .NET Framework abstract the complexities involved with defining web services in terms of SOAP, WSDL, XSD schemas, and the like. You still have access and control over these low-level items if you need them. However, for the most part, you define a web service the same way you create a class. You then define web methods similar to the way you would define methods on a class. Visual Studio takes on the job of creating the appropriate schema contract for your method signatures and describing your web services in terms of WSDL. The .NET Framework then worries about properly packaging your data into a SOAP message and transmitting it across HTTP.

.NET Web Services

Web services in .NET are built and delivered on the same ASP .NET framework that delivers ASP .NET websites. This means that web services share the same server technology (Internet Information Server or IIS) as well as the same objects such as Application, Session, and Context. In this way, developers who are familiar with delivering ASP .NET applications can leverage this experience for web services. You can take advantage of the built-in state management, authentication services, and performance of ASP .NET, for instance.

A web service in .NET is simply a web address to a web service file that contains the extension .asmx. The service has a standard http:\\... address. This file is used as the URI for the web service (just like a form). You write your code "behind" this file, and Visual Studio and .NET do the rest by attaching the appropriate WSDL and enabling the SOAP calls over HTTP. Let's take a closer look at how to leverage Visual Studio and ASP .NET to deliver web services.

ASP.NET Web Service Project

A web service can be added to any ASP .NET web project. Visual Studio also provides a web servicespecific project template. This template is useful if you intend to create an entire service layer or want to separate your services from any user interface elements. You create a project from this template the same way you define other web projects: Selecting File, New, Website will launch the New Web Site dialog box, as shown in Figure 16.1. Here, you can select the template, ASP.NET Web Service, to define a web servicespecific project.

Figure 16.1. Create a web service project.


As with defining a new website, you can indicate the location of the web service. You can choose a location by using the file system or by browsing to a web server using HTTP, or by leveraging FTP to indicate where to store the web service. In addition, you can define the default language in which the web service should be programmed.

The Web Service Files

The actual web service project that is created through the web service template contains exactly one service. The service is referenced by an .asmx file. Again, this file is used as the URI (and URL) for the web service. The actual contents of this file are depicted in Figure 16.2.

Figure 16.2. The web service project.


The .asmx file in the figure contains a single WebService directive. This directive indicates that the web service file is a pointer to the code for the service. The CodeBehind attribute indicates this information. Here, it points to the code file called Service.cs inside the App_Code directory. This is the place where the application logic for the service resides. Finally, this directive uses the Class attribute to indicate a class contained inside Service.cs;. this class is the web service. The methods in this class are the web methods.

Note

You can actually choose to define a web service as a single file. In this case, both the directive and the code are stored together (instead of the code-behind model).


Figure 16.3 shows the contents of Service.cs. First, note that this class is just a standard C# class (it could also be VB). It has a constructor, using statements, a method, and so on. The web service programming model should be very familiar to .NET developers.

Figure 16.3. The web service code.


Of course, you still have to let .NET know that this code is a web service. There are two ways the code does this. First, it uses attributes. The Service class in Figure 16.3 is decorated with the WebService attribute. This defines the class as a web service. Notice also that the HelloWorld method has the WebMethod attribute applied. This attribute indicates this method is a web method that can be called for the web service.

Another way this class indicates itself as a web service is through inheritance. The class inherits System.Web.Services.WebService. This is actually optional. You could define a class as a web service without inheriting from WebService. However, through this inheritance, the class will actually have full access to the ASP.NET features such as Session and Context objects. Having access to these objects provides many of the familiar ASP.NET features to web service developers.




Microsoft Visual Studio 2005 Unleashed
Microsoft Visual Studio 2005 Unleashed
ISBN: 0672328194
EAN: 2147483647
Year: 2006
Pages: 195

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net