我正在尝试向wcf客户端添加自定义标头,以达到标准的RESTful端点。我试图添加某种标题,只允许我跟踪从一个层到下一个层的请求。这是我试图实现它的方式:
public class DynatracePurePathHeaderAppender : IClientMessageInspector, IEndpointBehavior
{
object IClientMessageInspector.BeforeSendRequest(ref Message request, IClientChannel channel)
{
var dynaHeader = MessageHeader.CreateHeader("Action", "ns.yellowbook.jeff", "dynatrace",false);
request.Headers.Add(dynaHeader);
return null;
}
void IClientMessageInspector.AfterReceiveReply(ref Message reply, object correlationState)
{
return;
}
public void Validate(ServiceEndpoint endpoint){}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters){}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher){}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(this);
}
}
public class DynatracePurePathHeaderAppenderElement : BehaviorExtensionElement
{
protected override object CreateBehavior()
{
return new DynatracePurePathHeaderAppender();
}
public override Type BehaviorType
{
get { return typeof(DynatracePurePathHeaderAppender); }
}
}
然后我成功配置了客户端,但是当它运行时,我得到以下异常:
System.InvalidOperationException:Envelope Version'EnvelopeNone (http://schemas.microsoft.com/ws/2005/05/envelope/none)'没有 支持添加消息标题。
有人对如何插入这种小钡餐有任何建议吗?
答案 0 :(得分:4)
我假设您的意思是HTTP标头而不是SOAP标头?如果是这样,MessageHeader与此无关。
尝试这样的事情:
HttpRequestMessageProperty hrmp = new HttpRequestMessageProperty();
//Set hrmp.Headers, then:
request.Properties.Add(HttpRequestMessageProperty.Name, hrmp);
通常,WCF REST支持在客户端并未真正优化(它主要是为了让人们创建 REST服务而创建的)。要获得更好的客户端REST支持,请查看WCF REST入门工具包中的HttpClient。
答案 1 :(得分:1)
在BeforeSendReply方法中,您需要获得响应:
var httpHeader = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
如果您有响应实例,则可以轻松添加所需的任何标题:
httpHeader.Headers.Add("my Custom Header", "My Value");
答案 2 :(得分:0)
我知道这已经很晚了,但我遇到了这个帖子所以决定填写这个,以防万一我需要再次记住这个。这对我有用:
创建消息检查器:
Public Class AuthenticationHeader
Implements IClientMessageInspector
Private itsUser As String
Private itsPass As String
Public Sub New(ByVal user As String, ByVal pass As String)
itsUser = user
itsPass = pass
End Sub
Public Sub AfterReceiveReply(ByRef reply As Message, correlationState As Object) Implements IClientMessageInspector.AfterReceiveReply
Console.WriteLine("Received the following reply: '{0}'", reply.ToString())
End Sub
Public Function BeforeSendRequest(ByRef request As Message, channel As IClientChannel) As Object Implements IClientMessageInspector.BeforeSendRequest
Dim hrmp As HttpRequestMessageProperty = request.Properties("httpRequest")
Dim encoded As String = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(itsUser + ":" + itsPass))
hrmp.Headers.Add("Authorization", "Basic " + encoded)
Return request
End Function
End Class
写下行为:
Public Class AuthenticationHeaderBehavior
Implements IEndpointBehavior
Private ReadOnly itsUser As String
Private ReadOnly itsPass As String
Public Sub New(ByVal user As String, ByVal pass As String)
MyBase.New()
itsUser = user
itsPass = pass
End Sub
Public Sub AddBindingParameters(endpoint As ServiceEndpoint, bindingParameters As BindingParameterCollection) Implements IEndpointBehavior.AddBindingParameters
End Sub
Public Sub ApplyClientBehavior(endpoint As ServiceEndpoint, clientRuntime As ClientRuntime) Implements IEndpointBehavior.ApplyClientBehavior
clientRuntime.MessageInspectors.Add(New AuthenticationHeader(itsUser, itsPass))
End Sub
Public Sub ApplyDispatchBehavior(endpoint As ServiceEndpoint, endpointDispatcher As EndpointDispatcher) Implements IEndpointBehavior.ApplyDispatchBehavior
End Sub
Public Sub Validate(endpoint As ServiceEndpoint) Implements IEndpointBehavior.Validate
End Sub
End Class
将其添加到您的终端:
Dim binding As New WebHttpBinding(WebHttpSecurityMode.Transport)
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None
ChlFactory = New WebChannelFactory(Of IMyServiceContract)(binding, New Uri(url))
ChlFactory.Endpoint.Behaviors.Add(New WebHttpBehavior())
ChlFactory.Endpoint.Behaviors.Add(New AuthenticationHeaderBehavior(user, pass))
Channel = ChlFactory.CreateChannel()