【问题标题】:HubConnection.Start throws error only when called from singleton objectHubConnection.Start 仅在从单例对象调用时引发错误
【发布时间】:2012-03-27 01:17:12
【问题描述】:

我用以下代码构建了一个通知系统:

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

当我从测试 Windows 窗体应用程序(单击按钮)触发它时,这很有效,但是当我从我拥有的单例对象调用它时,它在 Start().Wait() 上失败。我已经复制粘贴了代码并检查了很多次以确保没有错别字。

这是我的通知单例。它比 SignalR 位做得更多。它更新了数据库等等,但这里是相关部分:

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

我得到的异常:

System.AggregateException 消息:发生一个或多个错误 StackTrace:在 System.Threading.Tasks.Task.ThrowIfExceptional(布尔 includeTaskCanceledExceptions) 在 System.Threading.Tasks.Task.Wait(Int32 毫秒超时,CancellationToken 取消令牌) 在 System.Threading.Tasks.Task.Wait()

【问题讨论】:

  • 你遇到了什么异常?
  • 我已将例外添加到帖子中。
  • {"无法加载文件或程序集 'Newtonsoft.Json, Version=4.0.8.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' 或其依赖项之一。系统找不到指定的文件。" :"Newtonsoft.Json, Version=4.0.8.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed"}
  • 但是参考。确实存在。我使用 NuGet 安装它,因此它拥有所需的一切。

标签: c# signalr


【解决方案1】:

我终于明白了。我的通知系统是一个单独的库,我的可执行文件的 bin 没有获取 Newtonsoft.JSON dll。我将使用 nuget 的包添加到我的主要项目中,它就像一个魅力。 @M.Babcock 感谢您带领我朝着正确的方向前进。我查看了异常,但我正在查看一个说“InnerExceptions”的异常(带有 s 的异常),它没有任何信息。但是当我查看“InnerException”时,我发现了更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多