我需要在发送到后端服务之前为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;
}
}
答案 0 :(得分:0)
问题通过以下解决方案解决。
禁用要路由到的服务或端点的SOAPProcessingBehavior(在路由配置中)。此链接告诉您如何操作:http://msdn.microsoft.com/en-us/library/ee816919.aspx