Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read WCF message headers in duplex callback?

In a normal WCF request/reply contract, you can read the message headers using something like:

OperationContract.Current.IncomingMessageHeaders

What I can't figure out is how to do this on the callback side of a duplex contract. Inside the callback implementation OperationContext.Current is null.

Edit 4/5/2013: I'm using a custom binding based on net.tcp, but with a lot of customizations. For example, using protocol buffers message encoding rather than Xml. Also there is some custom security.

like image 846
Simon Gillbee Avatar asked Oct 22 '22 13:10

Simon Gillbee


1 Answers

What binding are you using? In the SSCCE below the context is not null on the callback implementation.

public class StackOverflow_15769719
{
    [ServiceContract(CallbackContract = typeof(ICallback))]
    public interface ITest
    {
        [OperationContract]
        string Hello(string text);
    }
    [ServiceContract]
    public interface ICallback
    {
        [OperationContract(IsOneWay = true)]
        void OnHello(string text);
    }
    public class Service : ITest
    {
        public string Hello(string text)
        {
            ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
            ThreadPool.QueueUserWorkItem(delegate
            {
                callback.OnHello(text);
            });

            return text;
        }
    }
    class MyCallback : ICallback
    {
        AutoResetEvent evt;
        public MyCallback(AutoResetEvent evt)
        {
            this.evt = evt;
        }

        public void OnHello(string text)
        {
            Console.WriteLine("[callback] Headers: ");
            foreach (var header in OperationContext.Current.IncomingMessageHeaders)
            {
                Console.WriteLine("[callback]   {0}", header);
            }

            Console.WriteLine("[callback] OnHello({0})", text);
            evt.Set();
        }
    }
    public static void Test()
    {
        bool useTcp = false;
        string baseAddress = useTcp ? 
            "net.tcp://" + Environment.MachineName + ":8000/Service" :
            "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        Binding binding = useTcp ?
            (Binding)new NetTcpBinding(SecurityMode.None) :
            new WSDualHttpBinding(WSDualHttpSecurityMode.None)
            {
                ClientBaseAddress = new Uri("http://" + Environment.MachineName + ":8888/Client")
            };
        host.AddServiceEndpoint(typeof(ITest), binding, "");
        host.Open();
        Console.WriteLine("Host opened");

        AutoResetEvent evt = new AutoResetEvent(false);
        MyCallback callback = new MyCallback(evt);
        DuplexChannelFactory<ITest> factory = new DuplexChannelFactory<ITest>(
            new InstanceContext(callback),
            binding,
            new EndpointAddress(baseAddress));
        ITest proxy = factory.CreateChannel();

        Console.WriteLine(proxy.Hello("foo bar"));
        evt.WaitOne();

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
like image 127
carlosfigueira Avatar answered Nov 01 '22 13:11

carlosfigueira