Implementing WCF Services

Visual Studio 2010 offers some project templates for creating WCF projects. Table 41.2 lists them all.

Table 41.2 WCF Project Templates

image

This book covers the WCF Service Application template that is useful because it provides service self-hosting. Basically a WCF service cannot be run or consumed as a standalone and must be hosted inside a .NET application. Host applications can be of several types: Console applications, Internet Information Services, and ASP.NET Development Server are all valid host applications. The WCF Service Application template provides hosting inside the ASP.NET Development Server that ships with Visual Studio and is appropriate for local testing purposes. Select the File, New Project command, and in the New Project dialog select the WCF Service Application template, as shown in Figure 41.1. Name the new project as BookService and then click OK.

Figure 41.1 Creating a new WCF project.

image

The goal of the example is to offer a way for validating books’ information, such as ISBN code, title, and author. The service exposes a Book class representing a single book and a method named ValidateBook that provides the validation logic. Before writing custom code, taking a look at the auto-generated code is a good idea for understanding what WCF needs. Visual Studio 2010 generated a Web project visible in Solution Explorer. The new project contains the following files:

• IService1.vb, which defines the contract interface

• Service1.svc.vb (nested into Service1.svc as a code-behind file), which defines the class that implements the contract

• Service1.svc, which is the actual service that exposes data and that is be consumed by clients

• Web.config, which provides definitions for the ABC

Note on the Web.config File

With the .NET Framework 4.0 and Visual Studio 2010, the Web.config file does not contain a WCF metadata definition in case you use the default settings because they are considered as implicit. This is important with regard to the current example. If you decide instead to implement custom settings, the Web.config stores the metadata definition. Because configuration files in client applications reflect Web.config files from services, later in this chapter you see the client-side metadata definition mapping the implicit metadata of the current sample service.

Let’s take a look at the IService1.vb file, which is reported in Listing 41.1 and that defines a couple of contracts.

Listing 41.1 Auto-Generated Contracts

image

The IService1 interface is decorated with the ServiceContract attribute, meaning that it establishes what members the service defines and makes available to the public. The interface defines two methods, both decorated with the OperationContract attribute. Such attribute makes methods visible to the external world and consumable by clients. You need to remember that in WCF marking a method as Public is not sufficient to make it available to clients; it needs to be marked as OperationContract to be visible. Methods exposed by WCF services are also known as service operations, and this definition will be recalled in the next chapter with regard to WCF Data Services. Notice how the GetDataUsingDataContract method receives an argument of type CompositeType. This type is a custom class declared as DataContract, meaning that the WCF service can exchange data of this type. Members from this class also need to be marked as DataMember to be visible to the external world. As for service operations, marking a member as Public is not sufficient; you need to decorate members with the DataMember attribute. The Service1 class shows an example of implementing the contract and the logic for service operations. Listing 41.2 shows the auto-generated sample code.

Listing 41.2 Auto-Generated Contracts Implementation

image

The class just implements the contract interface and provides logic for service operations working like any other .NET class. The content of the .svc file is discussed later; for now let’s make some edits to the code replacing the auto-generated one with custom implementation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.181