将 C# WCF 扩展性代码移动到配置文件 [英] Move C# WCF Extensibility code to Configuration File

查看:20
本文介绍了将 C# WCF 扩展性代码移动到配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码将 ParameterInspector 添加到端点.

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();

作为一个很好的做法,我们正在尝试将所有这些代码移动到 Web.config.所以仅仅创建这样的工厂

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");

或者这个 -

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

应该从配置中获取扩展元素.通过以下配置,不会触发 IParameterInspectorBeforeCallAfterCall 方法.能否请您指出我们在遵循 Web.config 时的错误 -

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>

感谢您的帮助.

参考:Carlos 博客

分辨率

根据 Carlos 的回答,我采取了以下步骤来解决问题.

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

步骤 1. 创建从 BehaviorExtensionElement 派生的 OperationProfilerBehaviorElement 类.该类负责实例化实现 IEndpointBehavior

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);
    } }

第 2 步.这个类必须在 Web.config 中声明如下,

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

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

第 3 步.添加了端点行为,如下所示,

Step 3. Added Endpoint behavior as below,

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

步骤 4. 设置端点的 behaviorConfiguration 属性等于 InspectParameters,如下所示,

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" />

现在我可以在单个 C# 行中初始化工厂,并且默认情况下从 Web.config 添加了参数检查器

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");

ChannelFactory factory = new ChannelFactory("BasicHttpBinding_ITest");

推荐答案

中引用的类型 OperationProfilerEndpointBehavior/<behaviorExtensions> 配置的部分不应该是实现 IEndpointBehavior 的类 - 它应该是从 BehaviorElementExtension 继承的类型,并且该类是应该创建行为的一个.

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.

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

这篇关于将 C# WCF 扩展性代码移动到配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆