Creating Services with the WCF - Creating a New Service from Scratch
(Page 3 of 4 )
You're about to be introduced to the WCF service. This lab isn't your typical "Hello World"--it's "Hello Indigo"! In this lab, you will learn how to build a new WCF service and in the process learn the minimum requirements of service development and consumption. Here's a short list of things you'll accomplish:
Create a new service contract and service implementation
- Programmatically configure a service host, its endpoints, and bindings
- Create a client application and open a client channel proxy to invoke the service
Now, before you start thinking "been there, done that," this simple lab will be slightly different because I'm going to give you some practical design tips that ensure configurability and appropriate decoupling of service, host, and client. In addition, I'll be diving deeper into basic concepts such as services, service contracts, endpoints, bindings,ServiceHost, and channels.
Lab: Creating Clients and Services Programmatically
In this first lab, you will create a new solution with three projects: a service, a host, and a client. When you run the service host, you'll expose a single service endpoint. The client application will access service operations through that endpoint. You'll host the service in a console application and invoke the service using a manually constructed proxy. This lab will teach you the basic requirements for creating, hosting, and consuming a service with WCF.
Creating a new service
The first thing you will do is create a new service contract with a single operation and implement this contract on a new service type.
- In this lab, everything begins from scratch, so you'll start by creating a new Visual Studio solution. Open a new instance of Visual Studio 2005. Select File -> New -> Project, and from the New Project dialog, create a new Blank Solution in the <YourLearningWCFPath>\Labs\Chapter1 directory. Name the solutionServiceFromScratch. Click OK to create the empty solution.
- Create the service project. From Solution Explorer, right-click on the solution node and select Add -> New Project. Select the Class Library template, name the projectHelloIndigo, and make sure the location path matches the solution at <YourLearningWCFPath>\Labs\Chapter1\ServiceFromScratch. Click OK to create the new project.
- Now you will create your first service contract. From Solution Explorer, rename the project's only class file to Service.cs. Open this file in the code window.
Add a new interface namedIHelloIndigoServicein Service.cs. Add a single method to the interface,HelloIndigo, with the signature shown here:
public interface IHelloIndigoService
{
string HelloIndigo();
}
- Add a reference to theSystem.ServiceModelassembly. From Solution Explorer, right-click References and selectSystem.ServiceModelfrom the list. You'll also need to add the followingusingstatement to Service.cs:
using System.ServiceModel;
To turn this interface into a service contract, you'll need to explicitly decorate the interface with the
ServiceContractAttribute. In addition, each method should be decorated with the
OperationContractAttributeto include it in the service contract. In this case, you'll make
IHelloIndigoServicea service contract and expose
HelloIndigo()as its only service operation by applying these attributes as shown here:
[ServiceContract
(Namespace="http:// www.thatindigogirl.com/ samples/2006/06")]
public interface IHelloIndigoService
{
[OperationContract]
string HelloIndigo();
}Providing a namespace for theServiceContractAttributereduces the possibility of naming collisions with other services. This will be dicussed in greater detail in Chapter 2.- In the same file, create a service type to implement the service contract. You can modify the existing class definition, renaming it toHelloIndigoService. Then add theIHelloIndigoServiceinterface to the derivation list and implementHelloIndigo()with the following code:
public class HelloIndigoService : IHelloIndigoService
{
public string HelloIndigo()
{
return "Hello Indigo";
}
}
- Compile the service project.
At this point, you've created a service contract with a single operation and implemented it on a service type. The service is complete at this point, but to consume it from a client application, you will need to host it first.
Next: Hosting a service >>
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.
|
|