Switching from instancecontextmode.Single to PerCall Loop through event problem

26 Views Asked by At

I'm fairly new to using the WCF and trying to improve some old pieces of code (so forgive me if I'm asking some dumb questions). I have a WCF in singleton mode and to improve my response time, I want to switch to a per-call mode. My only problem is that inside my constructor I subscribe to one event that manages all the messages flow inside my system.

_controllers.ForEach(c =>
        {
            c.OnTelegramSent += Controller_OnTelegramSent;
            c.OnTelegramReceived += Controller_OnTelegramReceived;
            c.OnPublicVariablesUpdated += Controller_OnPublicVariablesUpdated;
        });

So after that while, I'm trying to manage this event with my method I keep looping for the same message multiple times.

private void Controller_OnTelegramReceived(object sender, GenericEventArgs e)
    {
        if (!e.Arguments[1].ToString().Contains("ACKT") && !e.Arguments[1].ToString().Contains("PING") && !e.Arguments[1].ToString().Contains("ASTA"))
        {
            ServicesMgr.Instance.NotifyTelegram(true, ((TrafficController)sender).Code, e.Arguments[1].ToString());

            Task.Run(() => SendNotifications((TrafficController)sender, e));
        }
    }

I don't even know if this is possible to use the per-call mode for this type of situation. One thing I noticed is that I subscribe multiple times to the same event and never unsubscribe, but I don't know how to manage that. I have the destructor where I unsubscribe from events but other than that I don't know what to do.

1

There are 1 best solutions below

0
Jiayao On

The per-call pattern works well with dense resources such as connection objects and large memory objects. And scalability is a prime requirement. Depending on your situation, try per-session first.