Services and the WCF - Defining a Service (Page 2 of 4 )
The first step in creating a service is to define a service contract. You create a service contract by applying the ServiceContractAttribute to an interface or type. Methods on the interface or type will not be included in the service contract until the OperationContractAttribute is applied. In a typical service contract, all methods will be included in the contract--after all, the entire reason for defining a service contract is to expose operations as part of a service. Business interfaces should not be directly converted into service contracts. Likewise, business components should not be directly converted to services. The service tier should instead be explicitly defined with the sole purpose of exposing public functionality and should internally consume business components, rather than embed business logic with the service implementation.
When you implement a service contract as an interface, the service type implements this interface. In this lab, the service implements a single service contract,IHelloIndigoService. This contract exposes a single operation,HelloIndigo().
An alternative to this approach is to apply both theServiceContractAttributeand theOperationContractAttributedirectly to the service type. Example 1-4 shows the changes you would make to the lab to achieve this. Here is a summary of those changes:
- When you apply theServiceContractAttributeto the service type, the service type name becomes the official name of the service contract. Thus, this is the name that must be provided when you create a new endpoint (seeAddServiceEndpoint()).
- On the client side, the service contract can still be represented as an interface (the client only requires metadata) but the name of that interface (the service contract) must match the new service contract name,HelloIndigoService--instead ofIHelloIndigoService. To update the lab, you can rename the interface at the client, or specify a value for theName property of theServiceContactAttributeas shown in Example 1-4. Service contracts are discussed in Chapter 2.
The following sample illustrates the coupling of service contracts with service type:<YourLearningWCFPath>\Sample\ServiceContracts\ ServiceContractOnServiceType.
Example 1-4. Changes that support defining the service contract with the service type
// HelloIndigo Project Service.cs
[ServiceContract(Namespace = http://www.thatindigogirl.com/samples/ 2006/06)]
public class HelloIndigoService
{
[OperationContract]
public string HelloIndigo()
{
return "Hello Indigo";
}
}
// Host Project - Program.cs
host.AddServiceEndpoint(typeof(HelloIndigo.HelloIndigoService), new BasicHttpBinding(),
"HelloIndigoService");
// Client Project - ServiceProxy.cs
[ServiceContract(Name="HelloIndigoService", Namespace = http://www.thatindigogirl.com/ samples/2006/06)]
public interface IHelloIndigoService
{
[OperationContract]
string HelloIndigo();
}
Hosting a Service
Any managed process can host services. Within that process, you can create one or more ServiceHost instances, each associated with a particular service type and exposing one or more endpoints for that type. This lab shows you how to host a service by creating an instance of the ServiceHost type for the HelloIndigoServicetype within a console application.
Before opening theServiceHostinstance, you can also provide it with base addresses if you are planning to create relative endpoints. In order to reach the service, at least one endpoint is required. To programmatically supply base addresses to theServiceHost, you can pass them to the constructor. TheServiceHosttype also provides anAddServiceEndpoint()method to create endpoints as shown here (from Example 1-1):
using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService),
new Uri(http://localhost:8000/HelloIndigo)))
{
host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService),
new BasicHttpBinding(), "HelloIndigoService");
// other code
}
In a simple scenario, theServiceHostneed only know its service type and associated endpoints where the service can be reached. This information is used to create a server channel that can receive and process messages. The channel is created when you call theOpen()method on theServiceHost instance. This creates a channel listener to receive messages for the service through its associated endpoints. The receiving channel processes incoming messages, invokes service operations, and processes responses. When theClose()method is called, the channel is gracefully disposed of after processing any remaining requests. In Example 1-1,Close()is automatically called when code block associated with theusingstatement ends.
The using statement can be applied to any type that implementsIDisposable. At the end of the using statement,Dispose()is called within atry...finallyblock to ensure cleanup even in the case of an exception.
Next: Exposing Service Endpoints >>
More BrainDump Articles
More By O'Reilly Media
|
This article is excerpted from chapter 1 of the book Learning WCF A Hands-on Guide, written by Michele Leroux Bustamante (O'Reilly, 2007; ISBN: 0596101627). Check it out today at your favorite bookstore. Buy this book now.
|
|