WCF - 发布者-订阅者 - 订阅问题

发布于 2024-08-06 23:42:29 字数 2662 浏览 6 评论 0原文

发布者接口

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

Publisher Interface

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

    }

Subscriber Interface

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

        [OperationContract]
        void UnSubscribe(string topicName);
    } 

Subscriber Program

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

Why am I getting this message?

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)."

This is my Host

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晒暮凉 2024-08-13 23:42:29

我认为,您的问题在这里:

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

订阅者服务主机是使用 IPublisherInterface 合约创建的,这是错误的。它应该是 ISubscriberInterface 代替

I think, your problem is here:

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

The Subscriber service host is created using IPublisherInterface contract, which is wrong. It should be ISubscriberInterface instead

偏闹i 2024-08-13 23:42:29

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

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

我在猜测名称,但我猜这可能是您为 ISubscriberInterface 实现指定的名称。

Are you sure with the second host opening? Shouldn't it be:

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

I'm guessing the names, but I guess that probably the name you given to your ISubscriberInterface implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文