WCF路由服务IClientMessageInspector --> BeforeSendRequest 未调用
在发送到后端服务之前,我需要为 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;
}
}
I need to add the HTTP header for each message in WCF Routing service before sending to back end service. I have implemented the below class. However when I debug "BeforeSendRequest " is not called hence HTTP header ic not added.
I noticed that "AfterReceiveRequest" is called and I added HTTP header but this is found that header is not sending to back ence server.
I need to add when "BeforeSendRequest " is called however , this is not triggering.
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该问题通过以下解决方案解决。
禁用您要路由到的服务或端点(在路由配置中)的 SOAPProcessingBehavior。此链接告诉您如何执行此操作: http://msdn.microsoft.com/ en-us/library/ee816919.aspx
The issue is solved with below solution.
Disable the SOAPProcessingBehavior for the service or the endpoint you are routing to (in your routing config). This link tels you how to do it: http://msdn.microsoft.com/en-us/library/ee816919.aspx