我正在努力在解决方案中设置基础结构,以发送和检索REST WCF服务的自定义标头。基本上,我们需要这样做来从客户端向服务发送UserID,密码,令牌值,如果提供的值有效,则操作将继续执行,否则将引发异常。
我们已经从IDispatchMessageInspector,IClientMessageInspector,IEndPointBehaviour,MessageHeader等接口继承了几个类,对于带有肥皂请求的WCF来说,这很好用。我尝试将这些类用于新的REST WCF服务,但由于MessageHeader派生的类仅支持Soap,因此无法正常工作。
我也尝试使用WebOperationContext,但没有运气:(
请提供示例解决方案以解决此问题。
非常感谢您!
答案 0 :(得分:0)
您的情况似乎更容易询问ASPNET管道
如果将以下内容添加到WCF服务中以允许其连接到ASPNET管道中
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
然后,您现在只需使用HttpContext对象,就可以像从普通aspnet应用程序中获取标头一样,例如
System.Web.HttpContext.Current.Request.Headers["CustomHeader"]
答案 1 :(得分:0)
如果要在wcf rest服务中添加http标头,则应使用HttpRequestMessageProperty,它具有Headers属性,可以通过其Headers属性设置http标头
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
HttpRequestMessageProperty property;
// if OutgoingMessageProperties already has HttpRequestMessageProperty, use the existing one , or initialize a new one and
set OutgoingMessageProperties's HttpRequestMessageProperty.Name key's value to the initialized HttpRequestMessageProperty so that the HttpRequestMessageProperty will work
if (OperationContext.Current.OutgoingMessageProperties.ContainsKey(HttpRequestMessageProperty.Name)){
property = OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
}
else
{
property = new HttpRequestMessageProperty();
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = property;
}
// add headers to HttpRequestMessageProperty, it will become the http header of the reuqest
property.Headers.Add(System.Net.HttpRequestHeader.Authorization, "myAuthorization");
string re = client.HelloWorld();
}
关于获取Header,只需使用WebOperationContext.Current.Headers。
WebOperationContext.Current.IncomingRequest.Headers["MyCustomHttpHeader"]
请参阅http://kenneththorman.blogspot.com/2011/02/wcf-rest-client-using-custom-http.html