我的winform WCF客户端有一个自定义的IEndpointBehavior,其中ApplyClientBehavior方法看起来像这样。
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(new CustomMessageInspector());
clientRuntime.CallbackDispatchRuntime.MessageInspectors.Add(new CustomMessageInspector());
}
為了得到serlized參數,我把IClientMessageInspector和IDispatchMessageInspector改為IParameterInspector。我如何将IEndpointBehavior转换为加载IParameterInspector?
我已经找到了这个。
SimpleServiceClient proxy = new SimpleServiceClient();
proxy.Endpoint.Contract.Operations[0].Behaviors.Add(new MyParameterInspector());
但是我的客户端是由CreateChannel创建的,所以Behaviors不存在,相反,有一个OperationsBehaviors,它不会加载IParameterInspector。
你可以在配置文件中配置它的行为,下面是一个演示。
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
foreach (ClientOperation op in clientRuntime.Operations)
op.ParameterInspectors.Add(new Inspector());
}
这就是ApplyClientBehavior。
public class Inspector : IParameterInspector
{
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
Console.WriteLine(
"IParameterInspector.AfterCall called for {0} with return value {1}.",
operationName,
returnValue.ToString()
);
}
public object BeforeCall(string operationName, object[] inputs)
{
Console.WriteLine("IParameterInspector.BeforeCall called for {0}.", operationName);
return null;
}
}
Inspector实现了IParameterInspector的接口。
<behaviors>
<endpointBehaviors>
<behavior name="Test">
<clientInterceptors/>
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add
name="clientInterceptors"
type="Client.InspectorInserter,Client, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
这是配置文件。
这是客户端运行的结果。
这里是参考链接。
https:/docs.microsoft.comen-usdotnetframeworkwcfextendinghow-toinspect或modify-parameters。
它是通过在每个操作中添加IParameterInspector来完成的,就像他的.NET Framework一样。
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
foreach (var clientOperation in clientRuntime.Operations)
clientOperation.ParameterInspectors.Add(new CustomMessageInspector());
foreach (var clientOperation in clientRuntime.CallbackDispatchRuntime.Operations)
clientOperation.ParameterInspectors.Add(new CustomMessageInspector());
}