简体   繁体   中英

WCF: Using Duplex for notifications across multiple instances of the same WCF service

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

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

[ServiceBehavior(
        InstanceContextMode = InstanceContextMode.Single)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM