服务器上的服务之间进行 WCF 通信

发布于 2024-08-19 21:23:39 字数 1640 浏览 3 评论 0 原文

我正在使用 WCF 回调在公司 LAN 内实现警报类型系统。它有订阅机制等。我使用过 教程作为起点,但我将绑定更改为 NetTcpBinding 而不是 wsDualHttpBinding,并且我在 Windows 服务中自行托管。

这工作得很好,但我有一个问题。我的大多数客户不需要回电。它们是各种桌面应用程序,只需要向服务器发送单向警报,该警报将被传递到运行启用回调的“通知”应用程序并订阅该类型警报的客户端。

我可能不关心这里的任何事情,但由于我的 WCF 服务实现了回调,因此所有客户端都需要实现回调对象,无论它们是否需要回调。如果客户端与不执行回调的服务进行通信的一种方式,这似乎是一个更整洁的解决方案。

所以...我在 WCF 服务中创建了另一个没有回调的端点。它只有一个简单的单向方法调用。这可行,但我的问题是我不太清楚如何将收到的消息传递到启用回调的服务。

我的 Windows 服务有这样的内容:

        internal static ServiceHost myNotifyHost;
    internal static ServiceHost mySendingHost;

    protected override void OnStart(string[] args)
    {
        // service with callback
        myNotifyHost = new ServiceHost(typeof(NotifyService));
        myNotifyHost.Open();

        // service without callback
        mySendingHost = new ServiceHost(typeof(SendingService));
        mySendingHost.Open();
    }

在 sendonly 客户端调用的 SendingService 方法中,我认为我能够执行以下操作:

    var notify = (NotifyService)WindowsService.myNotifyHost.SingletonInstance;

    notify.SendMessage("Message text");

SendMessage() 将回调消息发送到订阅的客户端。不幸的是,即使有客户端连接并等待回调,myNotifyHost.SingletonInstance 也始终为 null。我想我误解了该属性的含义。 NortifyService 具有这些属性

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]

有没有办法可以在两个服务之间进行通信?我是否应该放弃这一点,只坚持一项服务,并在那些不需要它的客户端中实现无意义的回调类。在这一点上,这没什么大不了的。这更多与我对WCF的理解有关。

谢谢

I'm implementing an alert type system within my company LAN using WCF callbacks. It has a subscribe mechanism etc. I've used this tutorial as a starting point but I changed the binding to NetTcpBinding instead of wsDualHttpBinding and I'm self hosting in a Windows service.

That's working quite nicely but I have a question. Most of my clients do not need callback. They are various desktop applications that only need to send a one way alert to the server which will be passed on to those clients running the callback enabled "Notify" application and subscribed to that type of alert.

I might be concerned about nothing here but since my WCF service implements callback, all the clients need to implement a callback object whether they need callback or not. It would seem like a more tidy solution if the one way clients communicated with a service that does not do callback.

So ... I created another endpoint without callback in my WCF Service. It just has a simple one way method call. That works but my problem is that I can't quite figure out how to pass the received message to the callback enabled service.

My Windows Service has something like this:

        internal static ServiceHost myNotifyHost;
    internal static ServiceHost mySendingHost;

    protected override void OnStart(string[] args)
    {
        // service with callback
        myNotifyHost = new ServiceHost(typeof(NotifyService));
        myNotifyHost.Open();

        // service without callback
        mySendingHost = new ServiceHost(typeof(SendingService));
        mySendingHost.Open();
    }

In my SendingService method that is called by the sendonly client, I thought I'd be able to do this:

    var notify = (NotifyService)WindowsService.myNotifyHost.SingletonInstance;

    notify.SendMessage("Message text");

SendMessage() sends the callback messages out to subscribed clients. Unfortunately, myNotifyHost.SingletonInstance is always null even when there is a client connected and waiting for callback. I guess I'm misunderstanding what that property means. NortifyService has these attributes

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]

Is there a way that I can communicate between the two services? Should I give up and this and just stick to the one service and just live with implementing the meaningless callback class in those clients that don't need it. At this point it's not a big deal. It's more to do with my understanding of WCF.

Thanks

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

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

发布评论

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

评论(1

夜空下最亮的亮点 2024-08-26 21:23:39

试试这个,

  public class NotifyService
    {
        public static NotifyService DefaultInstace;

        public NotifyService()
        {
            DefaultInstace = this;


        }
     ///.....SNIP......      
   }

Try this,

  public class NotifyService
    {
        public static NotifyService DefaultInstace;

        public NotifyService()
        {
            DefaultInstace = this;


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