David
David

Reputation: 2272

WCF service multiple behaviors

I've done some WCF, but it's been a while, and I've never had to configure a service from scratch.

Apparently, while you can only have one behavior configuration, it's possible to attach multiple behaviors to a WCF service:

ServiceHost serviceHost = new ServiceHost(typeof(Services.FooService), ServiceEndpointUri);
WebHttpBinding binding = new WebHttpBinding();       
ServiceEndpoint sep = serviceHost.AddServiceEndpoint(typeof(Contracts.IFooService), binding, string.Empty);
sep.Behaviors.Add(new WebHttpBehavior());
sep.Behaviors.Add(new MyCustomEndpointBehavior());

I'm wondering:

  1. Is it possible to attach multiple service behaviors to a service? ... or, for that matter, multiple endpoint behaviors? Or are you limited to at most one of each? That's not clear to me from this snippet (which is the closest thing to an answer I've turned up so far in my search).
  2. Is it possible to do this via configuration (rather than programmatically)?

Upvotes: 0

Views: 2344

Answers (1)

Scott Chamberlain
Scott Chamberlain

Reputation: 127603

You just create a named behavior then list your behaviors as sub-items of your named behavior.

  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="MyExampleBehavior">
          <webHttp />
          <MyCustomEndpointBehavior />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="Example.Endpoints.MyEndpoint">
         <endpoint behaviorConfiguration="MyExampleBehavior" .../>
    </service>
  </services>

using SvcConfigEditor.exe (which should have been included with your visual studio install under the name "Service Configuration Editor") it can help you edit and configure .config files and create complex bindings the one above easily.

enter image description here

Upvotes: 2

Related Questions