0

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

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。所以仅仅像这样创建工厂

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

或这个 -

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

应该从配置中获取扩展元素。通过以下配置,不会触发IParameterInspector的BeforeCallAfterCall方法。您能否指出我们在遵循 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>

谢谢您的帮助。

参考: 卡洛斯博客

编辑:分辨率

根据卡洛斯的回答,我采取了以下步骤来解决这个问题。

步骤 1. 创建派生自 BehaviorExtensionElement 的 OperationProfilerBehaviorElement 类。该类负责实例化实现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 中声明如下,

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

步骤 3. 添加了如下端点行为

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

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

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

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

ChannelFactory factory = new ChannelFactory("BasicHttpBinding_ITest");

4

1 回答 1

1

配置部分中OperationProfilerEndpointBehavior引用的类型不应该是实现的类- 它应该是继承自的类型,并且该类是应该创建行为的类。<extensions> / <behaviorExtensions>IEndpointBehaviorBehaviorElementExtension

在http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/28/wcf-extensibility-behavior-configuration-extensions.aspx查看有关行为扩展的更多信息。

于 2012-10-02T00:00:16.677 回答