在发送到后端服务之前,我需要为 WCF 路由服务中的每条消息添加 HTTP 标头。我已经实现了下面的类。但是,当我调试“BeforeSendRequest”时没有调用,因此没有添加 HTTP 标头 ic。
我注意到调用了“AfterReceiveRequest”并添加了 HTTP 标头,但发现标头没有发送到后端服务器。
但是,我需要在调用“BeforeSendRequest”时添加,这不会触发。
public class RouterMessageLogger : BehaviorExtensionElement, IClientMessageInspector, IEndpointBehavior, IDispatchMessageInspector
{
public override Type BehaviorType
{
get
{
return typeof(RouterMessageLogger);
}
}
protected override object CreateBehavior()
{
return new RouterMessageLogger();
}
#region IClientMessageInspector Members
**public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
Message MyMsg = request;
this.AddHTTPHeader(ref request);
//_Logging.LogMessage("Routing message to service");
return null;
}**
public void AfterReceiveReply(ref Message reply, object correlationState)
{
Message MyMsg = reply;
//_Logging.LogMessage("Response from service received");
}
#endregion
#region IDispatchMessageInspector Members
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
Message MyMsg = request;
this.AddHTTPHeader( ref request);
//_Logging.LogMessage("Message received from client");
return request;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
Message MyMsg = reply;
this.AddHTTPHeader(ref reply);
//_Logging.LogMessage("Sending response to client");
}
#endregion
#region IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
bindingParameters.Add(this);
//return;
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(this);
clientRuntime.CallbackDispatchRuntime.ImpersonateCallerForAllOperations = true;
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
}
public void Validate(ServiceEndpoint endpoint)
{
return;
}
}