wcf - 如何在双工回调中读取 WCF 消息头?

标签 wcf message duplex operationcontext

在正常的 WCF 请求/回复契约(Contract)中,您可以使用以下内容读取消息 header :

OperationContract.Current.IncomingMessageHeaders

我不知道如何在双工合约的回调端执行此操作。回调实现内部 OperationContext.Currentnull .

2013 年 4 月 5 日编辑:
我正在使用基于 net.tcp 的自定义绑定(bind),但有很多自定义项。例如,使用 Protocol Buffer 消息编码而不是 Xml。还有一些自定义安全性。

最佳答案

你用的是什么绑定(bind)?在 SSCCE 下面的回调实现中,上下文不为空。

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();
    }
}

关于wcf - 如何在双工回调中读取 WCF 消息头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15769719/

相关文章:

c# - 如何在 WCF 4.5 中使用 gzip 压缩

c# - 没有端点监听 wcf/rest

wcf - Net Messaging 绑定(bind)轮询间隔和并发性

node.js - 如何在 NodeJS 中编写双向双工流

.net - 使用高级选项打印(纸盘选择、双面打印、装订)

silverlight-4.0 - HttpPollingDuplex 的 serverPollTimeout 配置

c# - 初学者 WCF 问题 - 可消费的异步服务

java - 干净的消息.properties

java - 如何从消息数组中删除项目

c++ - 设置跨多个桌面广播消息的权限