Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HubConnection.Start throws error only when called from singleton object

Tags:

c#

signalr

I have built a notification system with the following code:

class SignalRClient
{
    HubConnection hubconn;
    IHubProxy proxy;

    public SignalRClient(string url)
    {
        hubconn = new HubConnection(url);
        proxy = hubconn.CreateProxy("XXX.NotificationHub");
        hubconn.Start().Wait();
    }

    public void SendMessage()
    {
        proxy.Invoke("LiveNotify", new { Application = "SRWinClient", Server = Environment.MachineName, Message = "This is a test", ImgId= 2 });
    }
}

This works great when i trigger it from a test windows forms app (on a button click), but when i call if from a singleton object that i have it fails on the Start().Wait(). I have copy pasted the code and checked a number of times to make sure there were no typos.

Here is my notification singleton. It does more than the SignalR bit. it updates Databases and more, but here is the relevant parts:

public class CDSNotifier
{
    private static object mLock = new object();
    private static CDSNotifier mnotifier = null;
    private bool enableSignalRNotifications = false;
    private SignalRNotifier snotifier;

    private CDSNotifier()
    {
        NameValueCollection appSettings = ConfigurationManager.AppSettings;
        try
        {
            enableSignalRNotifications = Convert.ToBoolean(appSettings["EnableSignalRNotifications"]);
        }
        catch { };

        if (enableSignalRNotifications)
        {
            snotifier = new SignalRNotifier(appSettings["SignalRHubURL"]);
        }
    }

    public static CDSNotifier Instance
    {
        get
        {
            if (mnotifier == null)
            {
                // for thread safety, lock an object when
                lock (mLock)
                {
                    if (mnotifier == null)
                    {
                        mnotifier = new CDSNotifier();
                    }
                }
            }
            return mnotifier;
        }
    } 
    public void Notify(int applicationId, int? companyId, string message, NotificationType type, string server)
    {
        .....

        try
        {
            if (enableSignalRNotifications)
                snotifier.SendMessage(applicationId, message, type);
        }
        catch { }
    }

Exception that I am getting:

System.AggregateException Message: One or more errors occured StackTrace: at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken) at System.Threading.Tasks.Task.Wait()

like image 897
mithun_daa Avatar asked Jul 19 '26 15:07

mithun_daa


1 Answers

I finally figured it out. My notification system was a separate lib and my executable's bin was not getting the Newtonsoft.JSON dlls. I added the package using nuget to my primary projects and it worked like a charm. @M.Babcock thanks for leading me in the right direction. i looked at the exceptions but i was looking at the one that said "InnerExceptions" (the one with s), that did not have any info. but when i looked in to "InnerException" i found more info.

like image 186
mithun_daa Avatar answered Jul 21 '26 04:07

mithun_daa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!