Building Blocks for a WCF Service Web Site (Page 1 of 4 )
In this seventh part of a ten-part series focusing on the Windows Communication Foundation (WCF), aka Indigo, you'll learn how to work with service and client behaviors, and start creating a WCF Service web site. 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). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
Working with Behaviors
As you've seen, the ServiceHost is initialized by the <service> configuration section associated with its service type. At least one endpoint must be configured for the service to be useful to clients. In this lab, a single service endpoint and a metadata exchange endpoint are exposed--both over HTTP. While endpoints describe where to reach the service, which operations are available at the specified address, and what protocols are required--behaviors affect the service model locally at the client or service. What that means is that behaviors are not exposed as part of metadata, and they are not shared between clients and services. Instead, they locally affect how the service model processes messages.
Behaviors can be defined in configuration or in code. Different behaviors are available to clients and services, since the local affect on the service model also differs.
Service behaviors
Service behaviors are types that implement IServiceBehavior from the
System.ServiceModel.Description namespace. There are service behaviors to control debugging, metadata, security features, serialization, and throttling. When enabled, each behavior interacts with the service model to achieve its goal. For example, when the metadata behavior is enabled, the service model will allow requests to a metadata exchange endpoint. Otherwise, it will not.
Service behaviors are configured in the<serviceBehaviors>section. The following example illustrates enabling service debug and service metadata behaviors:
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata />
</behavior>
</serviceBehaviors>
</behaviors>
To associate a set of behaviors with a service use thebehaviorConfigurationattribute of the<service>section (see Example 1-8).
Example 1-8. Associating a service behavior to a service
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="serviceBehavior" name="Host.HelloIndigoService" >
...
</service>
</services>
</system.serviceModel>
You may forget to make the association between the service and behavior at least a few times. Don't forget to double-check your configuration when you aren't seeing the expected results at runtime!
You can also programmatically configure service behaviors through theServiceHostinstance. TheDescriptionproperty of theServiceHosthas aBehaviorscollection. You can see if a behavior exists by calling theFind<T>()method on the collection. You can add new behaviors by callingAdd()on the collection. Example 1-9 shows an example that looks to see if theServiceMetadataBehaviorexists, and if not adds it to the collection and enables browsing.
Example 1-9. Adding the metadata service behavior programmatically
ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService);
ServiceMetadataBehavior mexBehavior = host.Description.Behaviors. Find<ServiceMetadataBehavior>();
if (mexBehavior == null)
{
mexBehavior = new ServiceMetadataBehavior();
mexBehavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(mexBehavior);
}
host.Open();
Next: Client behaviors >>
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.
|
|