Reputation: 12129
Given these two classes:
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;
}
}
And this behavior initialization code:
var requestInterceptor = new InspectorBehavior();
MYSoapClient.Endpoint.Behaviors.Add(requestInterceptor);
Why is it that ApplyClientBehavior
method is never executed and MessageInspector
is always null?
Shouldn't ApplyClientBehavior
be executed when the behavior is added to Endpoint Behavior collection (I verified that it was and that requestInterceptor variable is not null) ?
Upvotes: 5
Views: 1837
Reputation: 601
In my case, I got this issue using wcf for .net core.
When I was setting the EndpointBehaviors property after the creation of the wcf client, the method ApplyClientBehavior was not being called.
What did the trick for me is: I had to set the EndpointBehaviors property inside the wcf client constructor. I can't say the reason why this did the trick though, it simply does not make sense to me, but it worked.
You can create a new partial class and have a new constructor to register this behavior if needed. This may be useful in case you need to regenerate the service contract.
Hope this can help someone else.
Upvotes: 5