Generating Clients and Services with Indigo - Generating a proxy with Add Service Reference
(Page 3 of 4 )
It's time to generate code for the client to consume the service, starting by generating a client proxy. To achieve this you will use the Add Service Reference functionality exposed by Visual Studio, which uses the ServiceModel Metadata Utility (SvcUtil) to generate a proxy and configuration settings for that proxy.
- Go to theClient project and from Solution Explorer, right-click on theClientproject node and select Add Service Reference. The dialog presented requires you to provide a valid base address to the service. Supply the base address http:// localhost:8000/HelloIndigo and leave the Service reference name aslocalhost. When you close this dialog, a service proxy and configuration file will be generated for the client application. Stop debugging so you can add code to the client application.
To see the proxy, go to theClientproject and expand the Service References folder. Beneath it you will see localhost.map, and beneath that localhost.cs--the latter of which contains the proxy.
A new configuration file, app.config, was also added to the project. This contains the service model configuration for the proxy. Later I'll talk about how these things come together.
- Add code to the client application to invoke the service using the generated proxy. Go to theClientproject and open Program.cs. Add code to theMain()entry point as shown in bold in Example 1-6.
- Compile the solution and run theHostproject, followed by theClient. The client's console output should show the result of invoking the service'sHelloIndigo operation.
Example 1-6. Using a generated proxy to invoke a service
static void Main(string[] args)
{
localhost.HelloIndigoServiceClient proxy = new
Client.localhost.HelloIndigoServiceClient()
string s = proxy.HelloIndigo();
Console.WriteLine(s);
Console.WriteLine("Press <ENTER> to terminate Client.");
Console.ReadLine();
}
This concludes one technique for generating a service,ServiceHostconfiguration, and a client proxy.
Creating a WCF Service Library
In this section, you will generate a service using another technique: adding a new class library that includes a WCF service. The WCF Service Library template is a quick and easy way to generate a new class library with a sample service contract, service type, and the appropriate assembly references.
- Go to the Solution Explorer and right-click on the solution node. Select Add -> New Project and select the WCF Service Library template. Name the projectHelloIndigo.
- Rename the class file from Class1.cs to HelloIndigoService.cs.
Modify the service contract that is supplied by the project template. Open
HelloIndigoService.cs in the code window and provide a namespace for the
ServiceContractAttributeand change the interface definition to look as follows:
[ServiceContract(Namespace=http://www.thatindigogirl.com/ samples/2006/06)]
public interface IHelloIndigoService
{
[OperationContract]
string HelloIndigo();
}Now, modify the service implementation so that it implements the new contract. Rename the service toHelloIndigoServiceand implementIHelloIndigoServiceas shown here:
public class HelloIndigoService: IHelloIndigoService
{
public string HelloIndigo()
{
return "Hello Indigo";
}
}
Compile theHelloIndigoproject.
The default data contract created when you used the WCF Service template is not necessary for this lab.
- Now you will modify the existing host project so that it hosts this new service. Go to theHostproject and open HelloIndigoService.cs. Comment the entire service contract and implementation to avoid collision with theHelloIndigolibrary you're about to reference.
- Add a reference to theHelloIndigoclass library project. Right-click on theHostnode and select Add Reference. From the Projects tab, select theHelloIndigoproject.
TheServiceHostmust be modified to refer to the service type from this project. In the HelloIndigoService.cs file, find theStartService()method and modify theServiceHost constructor to use the fully qualified name of the service,HelloIndigo.HelloIndigoService, as shown here:
myServiceHost = new ServiceHost(typeof(HelloIndigo.HelloIndigoService));
You'll also have to edit the service model section of the configuration file to use the correct service and contract types. Go to theHost project and open the app.config file. Change the service type and contract type for the<service>configuration section as shown here in bold:
<service behaviorConfiguration="serviceBehavior" name="HelloIndigo.
HelloIndigoService">
<endpoint address="HelloIndigoService" binding="basicHttpBinding" name="basicHttp" contract="HelloIndigo.IHelloIndigoService" />
<!-- other settings -->
</service>
- Test the solution again by compiling and running theHostand then theClient.
Now you have learned how to create a new class library with a sample WCF service and seen the changes required to the service model configuration andServiceHostto reference a different service type.
Next: Generating a proxy using the Service Model Metadata Utility >>
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.
|
|