次のコードは、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のBeforeCallまたはAfterCallメソッドはトリガーされません。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" />
これで、ファクトリを1つのC#行で初期化でき、パラメータインスペクタがデフォルトでWeb.configから追加されました。
ChannelFactoryファクトリ=newChannelFactory( "BasicHttpBinding_ITest");