Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If there's no header with that name / namespace, you'll get an exception (<code>MessageHeaderException</code>) in the call to GetHeader. If you don't want to deal with exceptions, you can first see if such a header exists (using the <code>FindHeader</code> method), then if it returns a non-negative value, get the corresponding header.</p> <pre><code>public class StackOverflow_8854137 { [ServiceContract] public interface ITest { [OperationContract] string Echo(string text); } public class Service : ITest { public string Echo(string text) { return text; } } class MyBehavior : IEndpointBehavior, IDispatchMessageInspector { public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { } public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { } public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this); } public void Validate(ServiceEndpoint endpoint) { } public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { try { Guid tokenId = request.Headers.GetHeader&lt;Guid&gt;("Token", "System"); Console.WriteLine("Token: {0}", tokenId); } catch (Exception e) { Console.WriteLine("{0}: {1}", e.GetType().FullName, e.Message); } return null; } public void BeforeSendReply(ref Message reply, object correlationState) { } } public static void Test() { string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), ""); endpoint.Behaviors.Add(new MyBehavior()); host.Open(); Console.WriteLine("Host opened"); ChannelFactory&lt;ITest&gt; factory = new ChannelFactory&lt;ITest&gt;(new BasicHttpBinding(), new EndpointAddress(baseAddress)); ITest proxy = factory.CreateChannel(); Console.WriteLine(proxy.Echo("No token")); using (new OperationContextScope((IContextChannel)proxy)) { OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Token", "System", Guid.NewGuid())); Console.WriteLine(proxy.Echo("With token")); } ((IClientChannel)proxy).Close(); factory.Close(); Console.Write("Press ENTER to close the host"); Console.ReadLine(); host.Close(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload