将HTTP标头添加到服务参考服务方法 [英] Adding http headers to a Service Reference service method
问题描述
我已经从WSDL生成了服务引用.我已经成功地根据服务引用对客户端进行了编码.我一直在使用serviceRef.serviceMethod(params ...)的模式来调用基于服务的方法.现在,我需要在发送的邮件中添加http标头.我找不到在哪里为服务的所有消息将它们设置为默认值,也找不到在调用某些方法时可以在何处设置它们的位置.一些文章建议我可以使用IClientMessageInspector,但是实现似乎很复杂.有什么建议吗?
I have generated a service reference from a WSDL. I have been coding a client against the service reference successfully. I have been using a pattern of serviceRef.serviceMethod(params...) to invoke the service based methods. Now I need to add http headers to my sent messages. I can't find where to set them as default for all messages for the service nor can I find where I can set them when invoking certain methods. Some articles suggest I could use IClientMessageInspector but the implementation seems complicated. Any suggestions?
推荐答案
从很多地方借来的钱,但大部分是:
Borrowing from many places but mostly:
我简化了,但是我想我有一个实现将添加自定义httpheader.
I simplified but I think I have an implementation that will add custom httpheaders.
public class HttpHeaderMessageInspector : IClientMessageInspector
{
private readonly Dictionary<string, string> _httpHeaders;
public HttpHeaderMessageInspector(Dictionary<string, string> httpHeaders)
{
this._httpHeaders = httpHeaders;
}
public void AfterReceiveReply(ref Message reply, object correlationState) { }
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
HttpRequestMessageProperty httpRequestMessage;
object httpRequestMessageObject;
if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
{
httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
foreach (var httpHeader in _httpHeaders)
{
httpRequestMessage.Headers[httpHeader.Key] = httpHeader.Value;
}
}
else
{
httpRequestMessage = new HttpRequestMessageProperty();
foreach (var httpHeader in _httpHeaders)
{
httpRequestMessage.Headers.Add(httpHeader.Key, httpHeader.Value);
}
request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
}
return null;
}
}
internal class HttpHeadersEndpointBehavior : IEndpointBehavior
{
private readonly Dictionary<string,string> _httpHeaders;
public HttpHeadersEndpointBehavior(Dictionary<string, string> httpHeaders)
{
this._httpHeaders = httpHeaders;
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { }
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
var inspector = new HttpHeaderMessageInspector(this._httpHeaders);
clientRuntime.MessageInspectors.Add(inspector);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }
public void Validate(ServiceEndpoint endpoint) { }
}
然后在更新我的服务参考之后:
Then after newing up my service reference:
var httpHeaders = new Dictionary<string, string>();
httpHeaders.Add("header1", "value1");
httpHeaders.Add("header2", "value2");
_serviceRef.Endpoint.Behaviors.Add(new HttpHeadersEndpointBehavior(httpHeaders));
没有其他改变.如果您想到了更简单的方法,请告诉我.
Nothing else had to change. If you think of a simpler way, let me know.
这篇关于将HTTP标头添加到服务参考服务方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!