Building Blocks for a WCF Service Web Site - Client behaviors
(Page 2 of 4 )
Client behaviors implement IEndpointBehavior, also from the System.ServiceModel.Description namespace. There are client behaviors to control debugging, security, serialization, timeouts, and routing. Endpoint behaviors interact with the service model at the client. Endpoint behaviors are configured in the <endpointBehaviors> section. The following example enables exception debugging for callbacks:
<behaviors>
<endpointBehaviors>
<behavior>
<callbackDebug includeExceptionDetailInFaults="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
Endpoint behaviors are associated with client endpoints using thebehaviorConfigurationattribute of the<endpoint>section (see Example 1-10).
Example 1-10. Associating an endpoint behavior to a client endpoint
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="clientBehavior">
...
</behavior>
</serviceBehaviors>
</behaviors>
<client>
<endpoint address="net.tcp://localhost:9000/" binding="netTcpBinding" contract="Client.localhost.IHelloIndigoService" behaviorConfiguration="clientBehavior">
</endpoint>
</client>
</system.serviceModel>
To programmatically configure endpoint behaviors, you can use the object model of the client proxy. TheEndpointproperty of the client proxy has aBehaviorscollection through which you can search for existing behaviors and add behaviors--similar to the way you would for theServiceHost. Example 1-11 shows an example that looks to see if theServiceDebugBehaviorexists, and if not adds it to the collection.
Example 1-11. Adding the debug service behavior programmatically
HelloIndigoServiceClient proxy = new HelloIndigoServiceClient();
ServiceDebugBehavior debugBehavior = proxy.Endpoint.Behaviors.Find<ServiceDebugBehavior>();
if (debugBehavior == null)
{
debugBehavior = new ServiceDebugBehavior();
debugBehavior.IncludeExceptionDetailInFaults = true;
proxy.Endpoint.Behaviors.Add(debugBehavior);
}
I will explore other behaviors for the client and service throughout this book as I review features related to each behavior. At this point, I want you to understand how services and client endpoints are related to behaviors.
Next: Proxy Initialization >>
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.
|
|