Generating Clients and Services with Indigo
(Page 1 of 4 )
In this fifth part of a ten-part series on the Windows Communication Foundation (WCF), aka Indigo, you'll learn how to use tools to generate clients and services. 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). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
Lab: Using Tools to Generate Clients and Services
In this lab, you will generate service code using two approaches: by adding a service to an existing host and by generating a new service library, both using Visual Studio templates. To configure service endpoint for the host, this time you'll use the Service Configuration Editor. To generate client proxies and related configuration you'll use the ServiceModel Metadata Utility (SvcUtil). Both of these tools are available through the Solution Explorer in Visual Studio.
Visual Studio extensions for WCF will be part of the next release of Visual Studio, code-named "Orcas." As such, the user interfaces and features of these extensions may change from the time of this writing.
Using the WCF Service template
In this first section of the lab, you'll create a new service using the WCF Service template and add it to an existing project. This template will add a sample service contract and service type to the project, along with the required service model assembly references. Since you will be adding the WCF Service to an executable project (the host), the template will also generate some code for hosting the service.
- Start by opening an existing Visual Studio solution that contains two projects: a shell console client and host. The solution is located at<YourLearningWCFPath>\ Labs\Chapter1\HelloIndigo\HelloIndigo.sln.
- First, you will add a new service to the host project. From Solution Explorer, right-click on theHostproject node and select Add -> New Item. Select the WCF Service template and name the file HelloIndigoService.cs.
Open
HelloIndigoService.cs in the code window and add a namespace qualifier for the service contract, then modify the service operation name and signature to match the following code in bold:
[ServiceContract(Namespace=http://www.thatindigogirl.com/ samples/2006/06)]
public interface IHelloIndigoService
{
[OperationContract]
string HelloIndigo();
}Modify the service implementation in the same file to implement the correct operation signature. This is how the resulting service type should look:
public class HelloIndigoService : IHelloIndigoService
{
public string HelloIndigo()
{
return "Hello Indigo";
}
}
The WCF Service template also generated a helper class for hosting the service, namedMyServiceHost, located beneath the service type. You're going to edit this class and remove the hardcoded base address provided to theServiceHostinstance.
Locate theStartService()method inMyServiceHostand modify it so that a base address is no longer passed to theServiceHost constructor. The resulting changes are shown here:
internal static void StartService()
{
myServiceHost = new ServiceHost(typeof(HelloIndigoService));
myServiceHost.Open();
}
You're going to use the hosting helper class to initialize theServiceHost for the service. Go to theHostproject and open Program.cs. Modify theMain()entry point so that it looks as follows:
static void Main(string[] args)
{
try
{
MyServiceHost.StartService();
Console.WriteLine("Press <ENTER> to terminate the host application");
Console.ReadLine();
}
finally
{
MyServiceHost.StopService();
}
}
- Compile theHostproject.
At this point, you have defined a service inside theHostproject and added code to host the service, but the implementation is incomplete. TheServiceHostrequires at least one endpoint before clients can invoke the service.
Next: Configuring service endpoints using the Service Configuration Editor >>
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.
|
|