Using Service Templates with Indigo - ServiceHost Initialization
(Page 3 of 4 )
The first lab illustrates how to configure the ServiceHost programmatically. This lab illustrates how to configure the ServiceHost declaratively using the service model configuration section. But how does the ServiceHost know which configuration section to use? When the ServiceHost is opened, it reads the <services>section looking for a<service>element that matches its service type. From the lab, consider thisServiceHostconstructor:
ServiceHost myServiceHost = new ServiceHost(typeof(HelloIndigo.HelloIndigoService));
TheServiceHostwill look for a<service>section using the nameHelloIndigo.HelloIndigoService, as shown here:
<service behaviorConfiguration="serviceBehavior"
name="HelloIndigo.HelloIndigoService" >
<host>...</host>
<endpoint... />
<endpoint... />
</service>
The<service>element can include base addresses and service endpoints, as shown previously in Example 1-5. You can supply a base address for any protocol so that you can expose relative service endpoints over that protocol. The following illustrates the<host>section with base addresses for HTTP, TCP, and named pipe protocols:
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/HelloIndigo" />
<add baseAddress="net.tcp://localhost:9000/HelloIndigo" />
<add baseAddress="net.pipe://localhost/HelloIndigo" />
</baseAddresses>
</host>
One or more<endpoint>sections may also be provided. As discussed previously, an endpoint is defined by an address, contract, and binding. If address is omitted altogether, the base address for the related binding protocol is used (and required). If the address omits the full URI, it is appended to the base address matching the binding protocol. However, you can specify a complete address that ignores the base address. The following illustrates these three choices for an endpoint configuration:
<endpoint binding="basicHttpBinding" name="basicHttp" contract="Host.
IHelloIndigoService" />
<endpoint address="HelloIndigoService" binding="basicHttpBinding" name="basicHttp"
contract="Host.IHelloIndigoService" />
<endpoint address="http://localhost:8001/HelloIndigo/
HelloIndigoService"
binding="basicHttpBinding" name="basicHttp" contract="Host.IHelloIndigoService" />
Endpoints have to be unique for a particular service. When multiple endpoints are exposed by a service, they must differ in address, contract, or transport protocol.
There are several reasons why a service may expose multiple endpoints, including the following:
- The service implements multiple contracts, each requiring its own endpoint
- The same or different service contracts must be accessible over multiple protocols
- The same or different service contracts must be accessible by clients with different binding requirements, possibly related to security, reliable messaging, message size, or transactions
These topics will be explored throughout the book.
Next: Enabling Metadata Exchange >>
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.
|
|