WCF Duplex - Push different notifications to each client?

后端 未结 2 709
谎友^
谎友^ 2020-12-19 12:44

I am designing a 2 WCF services that all my clients will connect to. One of these services will be a notifications service.

I would like each client to connect to th

相关标签:
2条回答
  • 2020-12-19 13:16

    In any way with duplex communication, you need to maintain TCP channel opened from server to client to be able to send notification.

    Client is the one who initiate connection to server, and you need to keep this connection open, if this connection is lost you can't (shouldn't) initiate connection from server to client, because client can be behind NAT, have firewall, etc.

    So in any way there must be some static (singleton) object on server side that keeps clients connections list, thought this is not necessarily WCF service. You can dependency inject this object into service constructor.

    public class ProductRepository
    {
        private EventAggregator eventAggregator;
    
        public void Add(Product product)
        {
            //...
            eventAggregator.Publish(new NewProductEvent(product))
        }
    }
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
    public class EventPublishingService
    {
        private IClientCallback client;
        private EventAggregator eventAggregator;
        private Func<Product, bool> predicate;
    
        public EventPublishingService(...)
        {
            eventAggregator.Subscibe<NewProductEvent>(OnNewProduct);
        }
        private void OnNewProduct(NewProductEvent e)
        {
            if (predicate(e.Product)==true) client.Notify(e.Product);
        }
    
        public void Subscribe()
        {
            client = OperationContext.Current.GetCallbackChannel<IClientCallback>()
            var user = ServiceSecurityContext.PrimaryIdentity;
            predicate = GetFilterForUser(user);
        }
    }
    
    0 讨论(0)
  • 2020-12-19 13:34

    What I meant to say is the following.

    Create a wcf service that will be called by each client for subscribing to filters once. The wcf service itself will simply add data to the database and include information such as client name and filters information in the data store. Then your worker thread which will be in a window service which will simply poll your db for data and when data is available it will read from the subscription tables. It will then push the data to the queue of each clients which could be a shared queue server like rabbitmq.

    On the client side assuming it is a window based app, it will simply poll the rabbitmq queue server for it data by looking for it in the queue with the name of itself(client1, e.t.c).

    0 讨论(0)
提交回复
热议问题