【发布时间】: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 安装它,因此它拥有所需的一切。