.net 将行为添加到端点行为后,IEndpointBehavior ApplyClientBehavior方法未执行
鉴于这两类:.net 将行为添加到端点行为后,IEndpointBehavior ApplyClientBehavior方法未执行,.net,wcf,soap,.net,Wcf,Soap,鉴于这两类: public class InspectorBehavior : IEndpointBehavior { public MessageInspector MessageInspector; public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters){}
public class InspectorBehavior : IEndpointBehavior
{
public MessageInspector MessageInspector;
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters){}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher){}
public void Validate(ServiceEndpoint endpoint){}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
MessageInspector = new MessageInspector();
clientRuntime.MessageInspectors.Add(MessageInspector);
}
}
public class MessageInspector : IClientMessageInspector
{
public string LastRequestXML { get; private set; }
public string LastResponseXML { get; private set; }
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
LastResponseXML = reply.ToString();
}
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
LastRequestXML = request.ToString();
return request;
}
}
此行为初始化代码:
var requestInterceptor = new InspectorBehavior();
MYSoapClient.Endpoint.Behaviors.Add(requestInterceptor);
为什么ApplyClientBehavior
方法从未执行,而MessageInspector
始终为空
当行为被添加到端点行为集合时,是否应该执行
ApplyClientBehavior
(我验证了它是,并且requestInterceptor变量不是null)?在我的例子中,我使用.net core的wcf解决了这个问题
在创建wcf客户端之后设置EndpointBehaviors属性时,没有调用方法ApplyClientBehavior
我的诀窍是:我必须在wcf客户端构造函数中设置EndpointBehaviors属性。我不能说为什么这样做的把戏,虽然它根本没有意义,我,但它的工作
如果需要,您可以创建一个新的分部类并使用新的构造函数来注册此行为。如果您需要重新生成服务契约,这可能很有用
希望这能帮助其他人。谢谢,有机会我会试试的。