Abhijeet
Abhijeet

Reputation: 13856

Move C# WCF Extensibility code to Configuration File

Following code adds ParameterInspector to the endpoint.

ChannelFactory<ITest> factory = new ChannelFactory<ITest>("BasicHttpBinding_ITest");
OperationProfilerManager clientProfilerManager = new OperationProfilerManager();
factory.Endpoint.Behaviors.Add(new OperationProfilerEndpointBehavior(clientProfilerManager));
ITest proxy = factory.CreateChannel();

As a good practice, We are attempting to move all this code to Web.config. So that merely creating factory like this

ChannelFactory<ITest> factory = new ChannelFactory<ITest>("BasicHttpBinding_ITest");

or this -

ChannelFactory<ITest> factory = new ChannelFactory<ITest>();

should fetch the extension elements from configuration. With following configurations, BeforeCall or AfterCall methods of IParameterInspector is not being triggered. Can you please point out our mistake in following Web.config -

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_ITest" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://n1:8000/Service" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_ITest" contract="ServiceReference1.ITest"
            name="BasicHttpBinding_ITest" />
    </client>
    <behaviors>
        <endpointBehaviors>
            <behavior name="todo">                
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <extensions>
        <behaviorExtensions>
            <add name="OperationProfilerEndpointBehavior" type="SelfHostedServiceClient.OperationProfilerEndpointBehavior, SelfHostedServiceClient"/>
        </behaviorExtensions>
    </extensions>
</system.serviceModel>

Thank you for your help.

Reference: Carlos blog

EDIT: Resolution

Based on Carlos answer, I took following steps to resolve the issue.

Step 1. Created OperationProfilerBehaviorElement class derived from BehaviorExtensionElement. This class is responsible for instantiating the class implementing IEndpointBehavior

class OperationProfilerBehaviorElement : BehaviorExtensionElement  {
    public override Type BehaviorType
    {
        get {
            return typeof(OperationProfilerEndpointBehavior);
        }
    }

    protected override object CreateBehavior()
    {
        OperationProfilerManager clientProfilerManager = new OperationProfilerManager();
        return new OperationProfilerEndpointBehavior(clientProfilerManager);
    } }

Step 2. This class had to be declared in Web.config as below,

<extensions>
  <behaviorExtensions>
    <add name="OperationProfilerBehavior" type="SelfHostedServiceClient.OperationProfilerBehaviorElement, SelfHostedServiceClient"/>
  </behaviorExtensions>
</extensions>

Step 3. Added Endpoint behavior as below,

<behaviors>
  <endpointBehaviors>
    <behavior name="**InspectParameters**">
      <OperationProfilerBehavior/>
    </behavior>
  </endpointBehaviors>
</behaviors>

Step 4. Set behaviorConfiguration attribute of the endpoint equal to InspectParameters as below,

  <endpoint address="http://localhost:8000/Service" behaviorConfiguration="InspectParameters"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ITest"
    contract="ServiceReference1.ITest" name="BasicHttpBinding_ITest" />

Now I was able to initialize factory in a single C# line and parameter inspector was added by default from Web.config

ChannelFactory factory = new ChannelFactory("BasicHttpBinding_ITest");

Upvotes: 0

Views: 624

Answers (1)

carlosfigueira
carlosfigueira

Reputation: 87228

The type OperationProfilerEndpointBehavior which is referenced in the <extensions> / <behaviorExtensions> section of the config should not be a class implementing IEndpointBehavior - it should be a type which inherits from BehaviorElementExtension, and that class is the one which should create the behavior.

See more information about behavior extensions at http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/28/wcf-extensibility-behavior-configuration-extensions.aspx.

Upvotes: 1

Related Questions