WCF Extensibility - Part 2

Tags: .NET, NETFX3, SOA, WCF

Service Description

 Service Description at Runtime

Figure 2. ServiceDescription at runtime

 

The Service Description is the heart of the service and is created when a ServiceHost or client ChannelFactory is created to describe all aspects of the service, endpoints and behaviors. This description is a logical view of what the WCF runtime is going to build when the Open method on the ServiceHost is executed, and also is used to generate the WSDL and policy metadata for the service. This metadata is exposed through the ServiceDescription class and the Description property on the ServiceHost and can be changed, for example, by injecting behaviors. To build the ServiceDescription information the WCF runtime first reflects over the service types and then it loads the service configuration. This information is then used when you execute the Open method of the ServiceHost to build the runtime.

In the code shown below you can see how the service description information could be accessed and modified:

 

// Construct the ServiceHost
ServiceHost host = new ServiceHost(typeof(EchoService), new Uri(“http://localhost/echo”) );
// Add endpoints to description
host.AddServiceEndpoint(typeof(IEchoService), new BasicHttpBinding(), “”);
// You can access the Description property of the host
Console.Writeline(host.Description.ServiceType);
// And you can change things before calling Open
foreach( ServiceEndpoint endpoint in host.Description.Endpoints )
{
         // Add My behavior to all Http endpoints
         If( endpoint.Uri.Scheme == “http” )
                 endpoint.Behaviors.Add( new MyHttpBehavior() );
}
// Open the Host
Host.Open();

Next post: Building a Custom ServiceHost

Enjoy!

Andrés G Vettori
MCSE/MCSD/MCT
EDS Top Gun - .NET Capability Architect

Leader of the C# Community of the Microsoft Users Group Argentina

No Comments