So there is my problem: I can't get header from web service response by using generated wcf proxy (service reference) while services are asmx.
ManagerSoapClient client = new ManagerSoapClient(); client.Authenticate(...); using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) { //headers = null var headers = OperationContext.Current.IncomingMessageHeaders; } It's strange, because SOAP response has some header:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Header> <OperationResult xmlns="http://tempuri.org/"> <Status>Success</Status> </OperationResult> </soap:Header> <soap:Body> ... </soap:Body> </soap:Envelope> Ok, I added custom IClientMessageInspector to check headers:
public class OperationResultMessageInspector : IClientMessageInspector { public object BeforeSendRequest(ref Message request, IClientChannel channel) { return channel; } public void AfterReceiveReply(ref Message reply, object correlationState) { //It founds header at position 0!! int headerIndex = reply.Headers.FindHeader("OperationResult", "http://tempuri.org/"); } } So there is a header after all... But why I can't access it using OperationContext.Current.IncomingMessageHeaders?
Ok. Now I just want to save this header in some place to be able to access it after service call. I decided to use extensions IExtension<OperationContext>
So my code now is next:
public class ManagerProxy { public void Authenticate() { ManagerSoapClient client = new ManagerSoapClient(); client.Endpoint.Behaviors.Add(new OperationResultBehavior()); client.Authenticate(...); using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) { //headers = null var headers = OperationContext.Current.IncomingMessageHeaders; //header = null OperationResultContextExtension header = OperationContext.Current.Extensions.Find<OperationResultContextExtension>(); } } } public class OperationResultMessageInspector : IClientMessageInspector { public object BeforeSendRequest(ref Message request, IClientChannel channel) { return channel; } public void AfterReceiveReply(ref Message reply, object correlationState) { IClientChannel channel = (IClientChannel)correlationState; //It founds header at position 0!! int headerIndex = reply.Headers.FindHeader("OperationResult", "http://tempuri.org/"); XmlDictionaryReader reader = reply.Headers.GetReaderAtHeader(headerIndex); OperationResultContextExtension extension = new OperationResultContextExtension { SomeData = reader.ReadString() }; using (OperationContextScope scope = new OperationContextScope(channel)) { OperationContext.Current.Extensions.Add(extension); } reader.Close(); } } public class OperationResultContextExtension : IExtension<OperationContext> { public string SomeData { get; set; } public void Attach(OperationContext owner) { } public void Detach(OperationContext owner) { } } public class OperationResultBehavior : IEndpointBehavior { public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { } public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { } public void Validate(ServiceEndpoint endpoint) { } public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { clientRuntime.MessageInspectors.Add(new OperationResultMessageInspector()); } } It should work, but unexpectedly for me OperationContext.Current.Extensions.Find<OperationResultContextExtension>(); is null as well.
So there are my questions:
1. Why OperationContext.Current.IncomingMessageHeaders returns null while reply.Headers.FindHeader found header correctly inside AfterReceiveReply
2. Extension OperationResultContextExtension looks pretty smooth, why I get null during OperationContext.Current.Extensions.Find<OperationResultContextExtension>().
3. Is there some better/simpler or at least working well way to obtain this headers from response?