WCF:使用 Duplex 在同一 WCF 服务的多个实例之间发送通知

发布于 2024-07-18 05:39:59 字数 1486 浏览 6 评论 0原文

在应用程序 (WPF) 的多个实例之间共享 WCF 服务(SoapClient 类)的单个实例的最佳方法是什么?

我需要这样做,因为我需要启用带有回调的双工通信,因此我需要将应用程序“注册”到服务,以便使用该应用程序的其他用户在新用户登录时收到通知。


顺便说一句下面被删除了,因为我已经确认,为了使通知发挥作用,注册者需要注册到同一个 wcf 服务实例...因此现在我需要一种共享此实例的方法

我目前正在开发一个应用程序,每当有人登录该应用程序时,我需要某种方法来通知当前正在使用该应用程序的用户。

我尝试过使用 WCF Duplex 东西,但是我无法让它工作......我认为其背后的原因是因为通知和订阅需要发生在 WCF 服务的同一个实例上。

但由于这个应用程序将部署在多个用户的电脑上,我不能只共享这个 wcf 服务的一个实例,是吗? (或者我可以吗?)

有没有办法为所有用户共享 wcf 服务的公共实例(SoapClient)? 或者我以错误的方式处理这个问题?

目前,我正在通过类库通过公共属性访问 WCF 服务,该属性每次访问时都会发送 wcf 服务的新实例,我认为这就是通知无法在多个实例上工作的原因应用程序。


以下是客户端应用程序(wpf 应用程序)用来访问服务方法的方法(在类库中):

public static MusicRepo_DBAccess_ServiceClient GetService(object instanceContext)
{
    return new MusicRepo_DBAccess_ServiceClient(new InstanceContext(instanceContext), dualBinding, endpointAddress);  
}

public static MusicRepo_DBAccess_ServiceClient GetService()
{
    return new MusicRepo_DBAccess_ServiceClient(new InstanceContext(new WSGateway()), dualBinding, endpointAddress);
}

在主应用程序窗口中,我从上面传入的重载方法中获取一个新实例 < code>this 作为instanceContext 参数,并Open 它等待通知,但当另一个用户从应用程序的另一个实例登录时,我永远不会收到通知。

这就是我在服务登录方法中通知注册商的方式(摘录):

if (!_callbackList.Contains(newUser))
{
     _callbackList.Add(newUser);
 }
 _callbackList.ForEach(c => c.NotifyNewUserOnline(loggedInUser));

What is the best possible way to share a single instance of a WCF Service (the SoapClient class) across multiple instances of an application (WPF)?

I need to do this because I need to enable duplex communications with callbacks, so i need to "register the application" to the the service so that other users using the application will get notified whenever a new user logs in.


Btw the below is striked out because I have confirmed that for the notifications to work, the registrants need to register to the same wcf service instance...thus now I need a way to share this instance

I am currently developing an application and I need some way to inform the users that are currently using the application whenever someone logs in the application.

I have tried using the WCF Duplex thing, but and I can't get it to work...and I think the reason behind it is because notifications and subscriptions need to occur to the same instance of the WCF Service.

But since this application will be deployed on multiple users' pcs, I cannot share only one instance of this wcf service eh? (or can I ?)

Is there a way to share a common instance of a wcf service (the SoapClient) for all the users? Or am I going about this the wrong way?

Currently I'm accessing the WCF Service through a class library via a public property that sends a new isntance of the wcf service every time it is accessed, and I think that that is the reason on why the notifications are not working on multiple instances of the application.


The following are the methods (in the class library) that the client application (a wpf app) uses to gain access to the service methods:

public static MusicRepo_DBAccess_ServiceClient GetService(object instanceContext)
{
    return new MusicRepo_DBAccess_ServiceClient(new InstanceContext(instanceContext), dualBinding, endpointAddress);  
}

public static MusicRepo_DBAccess_ServiceClient GetService()
{
    return new MusicRepo_DBAccess_ServiceClient(new InstanceContext(new WSGateway()), dualBinding, endpointAddress);
}

In the main application window, I am then getting a new instance from the above overloaded method passing in this as the instanceContext parameter and the Open it to wait for the notifications but I am never notified when another user logs in from another instance of the application.

This is how I am notifying the registrars (excerpt) in the service login method:

if (!_callbackList.Contains(newUser))
{
     _callbackList.Add(newUser);
 }
 _callbackList.ForEach(c => c.NotifyNewUserOnline(loggedInUser));

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

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

发布评论

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

评论(1

花开半夏魅人心 2024-07-25 05:39:59

解决方案很简单。 我所需要的只是将 InstanceContextMode 更改为 Single

[ServiceBehavior(
        InstanceContextMode = InstanceContextMode.Single)]

The solution was simple. All I needed was to change InstanceContextMode to Single:

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