从 IClientMessageInspector 切换到 IParameterInspector?
Switch from IClientMessageInspector to IParameterInspector?
我的 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());
}
为了掌握序列化参数,我已将 IClientMessageInspector 和 IDispatchMessageInspector 更改为 IParameterInspector。如何转换 IEndpointBehavior 以改为加载此 IParameterInspector?
我找到了这个:
SimpleServiceClient proxy = new SimpleServiceClient();
proxy.Endpoint.Contract.Operations[0].Behaviors.Add(new MyParameterInspector());
然而,我的客户端是从 CreateChannel 创建的,因此 Behaviors 不存在,而是有一个不会采用 IParameterInspector 的 OperationsBehaviors。
这是通过将 IParameterInspector 添加到每个操作来完成的:
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());
}
你可以在配置文件中配置它的行为,这里是一个演示:
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>
这是配置文件。
这是客户端的结果运行。
这里是参考link:
https://docs.microsoft.com/en-us/dotnet/framework/wcf/extending/how-to-inspect-or-modify-parameters
我的 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());
}
为了掌握序列化参数,我已将 IClientMessageInspector 和 IDispatchMessageInspector 更改为 IParameterInspector。如何转换 IEndpointBehavior 以改为加载此 IParameterInspector?
我找到了这个:
SimpleServiceClient proxy = new SimpleServiceClient();
proxy.Endpoint.Contract.Operations[0].Behaviors.Add(new MyParameterInspector());
然而,我的客户端是从 CreateChannel 创建的,因此 Behaviors 不存在,而是有一个不会采用 IParameterInspector 的 OperationsBehaviors。
这是通过将 IParameterInspector 添加到每个操作来完成的:
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());
}
你可以在配置文件中配置它的行为,这里是一个演示:
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>
这是配置文件。
这是客户端的结果运行。
这里是参考link:
https://docs.microsoft.com/en-us/dotnet/framework/wcf/extending/how-to-inspect-or-modify-parameters