Multiple Service Contracts and Indigo - Hosting two services with multiple contracts
(Page 2 of 4 )
Now you will host both services in a single console application. This will require you to create two ServiceHost instances and provide two <service> configuration sections, one for each service type.
- First, make sure theHost project can access the service contracts and service types. Go to theHostproject and add assembly references to two projects:BusinessServiceContractsandBusinessServices.
In the application configuration file provided for theHost, provide configuration settings for both services. Open the app.config file and add the<system.serviceModel>section shown in Example 1-19. This section belongs inside the<configuration>section of the file.
The configuration section forServiceAexposes two endpoints for the service contractIServiceA: one for Internet access over HTTP, another for TCP access behind the firewall.ServiceBalso exposes two endpoints for the service contractIServiceB: one for Internet access and another for named pipe access restricting communications to the same machine.
Both services expose theIAdmincontract over TCP and named pipes, respectively, allowing callers on the same machine, or on remote machines behind the firewall. They also expose mex endpoints and enable the service metadata behavior for proxy generation.
Each service configuration also provides the appropriate base addresses for the protocols they support across all endpoints.
Example 1-19. Service model configuration for ServiceA and ServiceB
<system.serviceModel>
<services>
<service name="BusinessServices.ServiceA" behaviorConfiguration="serviceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000"/>
<add baseAddress="net.tcp://localhost:9000"/>
</baseAddresses>
</host>
<endpoint address="Admin" contract="BusinessServiceContracts.IAdmin"
binding="netTcpBinding" />
<endpoint address="ServiceA" contract="BusinessServiceContracts.IServiceA"
binding="basicHttpBinding" />
<endpoint address="ServiceA" contract="BusinessServiceContracts.IServiceA"
binding="netTcpBinding" />
</service>
<service name="BusinessServices.ServiceB" behaviorConfiguration="serviceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001"/>
<add baseAddress="net.pipe://localhost"/>
</baseAddresses>
</host>
<endpoint address="Admin" contract="BusinessServiceContracts.IAdmin"
binding="netNamedPipeBinding" />
<endpoint address="ServiceB" contract="BusinessServiceContracts.IServiceB"
binding="basicHttpBinding" />
<endpoint address="ServiceB" contract="BusinessServiceContracts.IServiceB"
binding="netNamedPipeBinding" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Each<service>section holds configuration settings for its own base addresses and endpoints. Recall that the configuration for a particular service is used to initialize aServiceHostinstance for that service type. Be mindful that base addresses across all sections must have unique ports since a port can be opened only once per machine.
Now that service model configuration has been provided for each service, you will write code to initialize and open aServiceHostinstance for both. Go to theHostproject and open Program.cs. Modify theMain()entry point so that itincludes the code shown in Example 1-20. You will also need to add ausing statement forSystem.ServiceModel.
This code creates two distinctServiceHost instances, one for each service. They are both constructed and opened within a try...finally block to ensure thatClose()is called for each when the host shuts down or if a fatal exception occurs.
Example 1-20. Initializing the ServiceHost for ServiceA and ServiceB
using System.ServiceModel;
static void Main(string[] args)
{
ServiceHost hostA = null;
ServiceHost hostB = null;
try
{
hostA = new ServiceHost(typeof(BusinessServices.ServiceA));
hostB = new ServiceHost(typeof(BusinessServices.ServiceB));
hostA.Open();
hostB.Open();
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate Host");
Console.ReadLine();
}
finally
{
hostA.Close();
hostB.Close();
}
}
- Compile and run theHostproject once to verify that no errors occur.
Because metadata browsing is enabled in the configuration section for each service type, you can browse to the WSDL document for each service by providing the HTTP base address for each service. Note that each service has its own distinct WSDL document, but for each individual service, all endpoints for the service are included in its WSDL document.
Next: Consuming internal services using shared contracts >>
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.
|
|