This post is part of a series about WCF extensibility points. For a list of all previous posts and planned future ones, go to the index page.
The first behavior covered in this series is the IServiceBehavior. It can be used to inspect or change the entire service description and runtime. For example, to collect information about all endpoints in the service, to apply some common behavior to all endpoints in the service, anything which affects the global service state, the service behavior is a good candidate. As you can see in the public implementations of the interface (and in the example later), they all apply to the service as a whole.
Public implementations in WCF
- AspNetCompatibilityRequirementsAttribute: Defines whether the service can, cannot or must use the ASP.NET Compatibility mode.
- ServiceAuthenticationBehavior: Defines the authentication behavior of the service, by providing an authentication manager to be called for incoming messages.
- ServiceAuthorizationBehavior: Defines properties for handling authorization for incoming messages to the service.
- ServiceCredentials: Defines the credentials used by the service, such as X.509 certificates.
- ServiceDebugBehavior: Defines debugging features for the service, such as the service help page or a flag indicating whether to send unknown exception details to clients.
- ServiceMetadataBehavior: Exposes metadata about the service for tools such as svcutil.exe or Add Service Reference.
- ServiceSecurityAuditBehavior: Defines the audit behavior of security events, such as authentication or authorization events.
- ServiceThrottlingBehavior: Defines throughput quotas to be applied to the service.
- UseRequestHeadersForMetadataAddressBehavior (new in 4.0): Used to determine that the machine names in metadata requests should use the address to which the request was made. Useful in scenarios where the server which is receiving the message is behind a load balancer – we want the endpoint addresses in the metadata to reflect the NLB’s address, not the individual node’s.
- ServiceDiscoveryBehavior (new in 4.0): Defines that the all the endpoints of this service will be announced using the WS-Discovery protocol.
- RoutingBehavior (new in 4.0): Defines the behavior for a service acting as a router.
- ServiceBehaviorAttribute: Allows the definition of some behavior for the service implementation.
Interface declaration
public interface IServiceBehavior
{
void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase);
void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters);
void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase);
}
As was mentioned in the post about Behaviors, you can use the Validate method to ensure that the service (or the host) doesn’t violate the validation logic on the behavior. One example is the AspNetCompatibilityRequirementsAttribute, which throws if the service requires ASP.NET compatibility, but it’s not there (i.e., in a self-hosted scenario), or if the behavior says that the compatibility mode is not allowed, but the hosting provides it.
Read more: Carlos' blog