【问题标题】:WCF - Publisher-Subscriber - Subscription ProblemWCF - 发布者-订阅者 - 订阅问题
【发布时间】:2009-10-01 10:04:18
【问题描述】:

发布者界面

[ServiceContract]
    public interface IPublisherInterface
    {
        [OperationContract(IsOneWay = false)]
        void Publish(MessageClass e, string topicName);

    }

订阅者界面

[ServiceContract(CallbackContract = typeof(IPublisherInterface))]
    public interface ISubscriberInterface
    {
        [OperationContract]
        void Subscribe(string topicName);

        [OperationContract]
        void UnSubscribe(string topicName);
    } 

订阅者计划

class Program
    {
        static void Main(string[] args)
        {
            DuplexChannelFactory<ISubscriberInterface> namedPipeFactory =
               new DuplexChannelFactory<ISubscriberInterface>(
                    new InstanceContext(new PublisherService()),
                  new NetNamedPipeBinding(),
                  new EndpointAddress("net.pipe://localhost/Sub"));

            ISubscriberInterface pipeProxy = namedPipeFactory.CreateChannel();
            pipeProxy.Subscribe("name");

            //SubscriberForm f = new SubscriberForm(pipeProxy);
            //f.ShowDialog();
        }
    }

为什么我会收到这条消息?

The message with Action 'http://tempuri.org/ISubscriberInterface/Subscribe' cannot be
 processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. 
This may be because of either a contract mismatch (mismatched Actions between sender and 
receiver) or a binding/security mismatch between the sender and the receiver.  Check that
 sender and receiver have the same contract and the same binding (including security 
requirements, e.g. Message, Transport, None)."

这是我的主人

class Program
    {
        static void Main(string[] args)
        {
            ServiceHost _publisherServiceHost = new ServiceHost(typeof(PublisherService), new Uri[] { new Uri("net.pipe://localhost/") });
            _publisherServiceHost.AddServiceEndpoint(
                    typeof(IPublisherInterface),
                    new NetNamedPipeBinding(),
                    "Pub");
            _publisherServiceHost.Open();

            ServiceHost _subscriberServiceHost = new ServiceHost(typeof(PublisherService), new Uri[] { new Uri("net.pipe://localhost/") });
            _subscriberServiceHost.AddServiceEndpoint(
                    typeof(IPublisherInterface),
                    new NetNamedPipeBinding(),
                    "Sub");
            _subscriberServiceHost.Open();

            Console.WriteLine("Server is Running.");
            Console.ReadLine();
        }
    }

【问题讨论】:

    标签: wcf publish-subscribe


    【解决方案1】:

    我想,你的问题就在这里:

    _subscriberServiceHost.AddServiceEndpoint(
                    typeof(IPublisherInterface),
                    new NetNamedPipeBinding(),
                    "Sub");
    

    Subscriber 服务主机是使用 IPublisherInterface 合约创建的,这是错误的。它应该是 ISubscriberInterface 而不是

    【讨论】:

      【解决方案2】:

      您确定第二个主机开放吗?不应该是:

          ServiceHost _subscriberServiceHost = new ServiceHost(typeof(SubscriberService), new Uri[] { new Uri("net.pipe://localhost/sub") });
          _subscriberServiceHost.AddServiceEndpoint(
                  typeof(ISubscriberInterface),
                  new NetNamedPipeBinding(),
                  "Sub");
          _subscriberServiceHost.Open();
      

      我在猜测名称,但我想这可能是您为 ISubscriberInterface 实现提供的名称。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-23
        • 2016-12-14
        • 1970-01-01
        • 2016-06-17
        • 1970-01-01
        • 2021-07-28
        相关资源
        最近更新 更多