So far in this chapter,I have shown you different ways to create services,
how to expose a service endpoint and metadata exchange endpoint,
how to generate client proxies,
how to work with metadata,
and how to configure service behaviors.
In this section,I’ll place the emphasis on endpoints,binding configuration,and allocation of assemblies for a more complex solution.
WCF includes a number of standard bindings that allow you to quickly configure a service for a particular set of protocols.
Clients must use compatible protocols when they communicate with each endpoint.
Services can expose multiple endpoints for the same service contract in order to expose functionality over different protocols.
For example,a service may be called by internal clients over TCP,but by external clients over HTTP.
In addition to supporting different protocols,internal and external clients may not have access to the same service contracts.
Some operations may be allowed only by clients within the domain,while others are publicly available to remote clients on the Internet.
In this lab you will configure multiple endpoints for a service to support different endpoint and binding configurations.
In the process you’ll explore the following concepts:
• Hosting multiple services
• Configuring multiple endpoints for a service
• Accessing a service from a Windows client application
• Initializing proxies from multiple endpoint and binding configurations
• Comparing proxy generation to sharing types between services and clients
Lab: Hosting Multiple Services and Sharing Types
In this lab,you’ll modify an existing solution to implement a service contract and an administrative contract on two distinct services.
You’ll then host each service in the same host process,a console application.
An internal client presumed to be behind the firewall will consume each service using network protocols such as TCP and named pipes.
This client will have access to service operations exposed by the service contract and administrative contract.
An external client,presumed to be accessing the service over the Internet will have access only to operations exposed by the service contract over HTTP.
The internal client will share class libraries to access service contracts,while the external client will use traditional methods for generating service proxies.
Implementing multiple contracts on a service
In this section,you’re going to implement the predefined service contracts on two distinct services.
Each service will expose two contracts: one for business functionality core to the service,the other for administrative functionality.
Both services will implement the same administrative contract.
This illustrates an example of contract factoring for reuse.
1. Start by opening the solution <YourLearningWCFPath>\Labs\Chapter1\MultiContractService\MultiContractService.sln.
This solution contains several shell projects,including a service library,a host,and two Windows client applications as follows:
BusinessServiceContracts
A class library containing three contracts: IAdmin, IServiceA,and IServiceB.
IAdmin defines administrative operations.
IServiceA and IServiceB respectively describe functionality to be exposed by ServiceA and ServiceB.
BusinessServices
A class library that will contain two services: ServiceA and ServiceB.
Host
A console application that will host ServiceA and ServiceB.
InternalClient
A windows client application that will access services behind the firewall.
ExternalClient
A windows client application that will access services over the Internet.
Note:Putting service contracts into a separate class library facilitates sharing metadata with client applications when you own both sides of the development effort.
2. The first thing you’ll do is provide an implementation for ServiceA,which is located in the BusinessServices class library.
First,take a look at the contracts you will implement.
Go to the BusinessServiceContracts project and open IServiceA.cs; you’ll see a contract with two operations.
Now open IAdmin.cs and you’ll see another contract with two different operations.
To implement these contracts,go to the BusinessServices project.
First,add a reference to the BusinessServiceContracts project so you can access the contracts it defines.
Then open ServiceA.cs and add a using statement for the BusinessServiceContracts namespace, as shown here:
using BusinessServiceContracts;
Modify the definition of ServiceA so that it derives from IServiceA and IAdmin as follows:
public class ServiceA : IServiceA, IAdmin
Implement both contracts implicitly.
You can use a shortcut by hovering your mouse over IServiceA and using the smart tag to select “Implement interface IServiceA,” as shown in Figure 1-29.