HubConnection.Start throws error only when called from a singleton object

I created 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 run it from a test windows app (button click), but when I call, if from one object I have, it doesn't work in Start (). Wait (). I copied the code and checked several times to make sure there were no typos.

Here is my singleton notification. This is more than a SignalR bit. it updates databases and more, but here are the relevant sections:

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 { }
    }

      

The exception I am getting:

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

+3


source to share


1 answer


I finally figured it out. My notification system was a separate library and my executable bin was not receiving DLL Newtonsoft.JSON. I added the package using nuget to my main projects and it worked like a charm. @ M.Babcock thanks me for guiding me in the right direction. I looked at the exceptions, but I looked at the one that said "InnerExceptions" (the one with s ), which didn't have any information. but when I looked at "InnerException" I found more information.



+2


source







All Articles