在多个项目中将 SqlDependency 与 SignalR 一起使用时无法找到程序集 System.Diagnostics.DiagnosticSource
Unable to find assembly System.Diagnostics.DiagnosticSource when using SqlDependency with SignalR in Multiple Projects
我在一个解决方案中有两个 asp.net MVC 项目。管理面板和网络。我需要使用 signalR 显示通知,并且每当通知 table 更新时。
为此,我将 SqlDependency 与 signalR 结合使用。
Web 项目运行良好,但管理项目给我这个错误
无法找到程序集 'System.Diagnostics.DiagnosticSource, Version=4.0.2.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'。
堆栈跟踪
[SerializationException: Unable to find assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.2.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +153
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +336
System._AppDomain.CreateInstance(String assemblyName, String typeName) +0
System.Data.SqlClient.SqlDependency.CreateProcessDispatcher(_AppDomain masterDomain) +62
System.Data.SqlClient.SqlDependency.ObtainProcessDispatcher() +54
System.Data.SqlClient.SqlDependency.Start(String connectionString, String queue, Boolean useDefaults) +1095
System.Data.SqlClient.SqlDependency.Start(String connectionString) +13
HelperForYourHome.Admins.Hubs.Startup.Configuration(IAppBuilder app) in C:\Users\user\Documents\Projects\HelperForYourHome\HelperForYourHome.Admin\Hubs\Startup.cs:26
[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +150
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +101
Owin.Loader.<>c__DisplayClass12.<MakeDelegate>b__b(IAppBuilder builder) +66
Owin.Loader.<>c__DisplayClass1.<LoadImplementation>b__0(IAppBuilder builder) +123
Microsoft.Owin.Host.SystemWeb.<>c__DisplayClass2.<InitializeBlueprint>b__0(IAppBuilder builder) +71
Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(Action`1 startup) +462
Microsoft.Owin.Host.SystemWeb.OwinBuilder.Build(Action`1 startup) +40
Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint() +70
System.Threading.LazyInitializer.EnsureInitializedCore(T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) +115
Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(HttpApplication context) +106
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +536
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +173
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296
[HttpException (0x80004005): Exception has been thrown by the target of an invocation.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +10042604
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +95
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254
具体实现如下
欧文创业公司
[assembly: OwinStartup("Startup", typeof(Startup))]
namespace Admins.Hubs
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
var idProvider = new CustomUserIdProvider();
GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => idProvider);
app.MapSignalR();
SqlDependency.Start(Connection.ConnectionString);
var properties = new AppProperties(app.Properties);
CancellationToken token = properties.OnAppDisposing;
if (token != CancellationToken.None)
{
token.Register(() =>
{
SqlDependency.Stop(Connection.ConnectionString);
});
}
}
}
}
两个项目都在使用这个启动文件
这是我的消息处理程序class
public class MessagesRepository
{
readonly string _connString = Connection.ConnectionString;
public IEnumerable<NotificationMessage> GetAllMessages()
{
var messages = new List<NotificationMessage>();
using (var connection = new SqlConnection(_connString))
{
connection.Open();
using (var command = new SqlCommand(@"SELECT [ID],[Title],[Description],[MessageType],[NotificationType],[Icon],[IsAjaxMessage],[IsViewMessage],[IsRedirectMessage] FROM [General].[NotificationMessage]", connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read())
{
messages.Add(new NotificationMessage(reader));
}
}
}
return messages;
}
public virtual void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<Notification>();
context.Clients.All.GetNotifications();
}
}
}
一切正常。除了这只在 Web 项目中工作正常而不在管理项目中工作。
我也遇到了同样的问题。解决方案是在我所有的项目中升级 System.Net.Http。
这是 nuget 命令:Update-Package System.Net.Http -Version 4.3.3
这是 nuget 网页:System.Net.Http nuget page
希望它也能帮助解决您的问题。
亲切的问候
我在一个解决方案中有两个 asp.net MVC 项目。管理面板和网络。我需要使用 signalR 显示通知,并且每当通知 table 更新时。 为此,我将 SqlDependency 与 signalR 结合使用。 Web 项目运行良好,但管理项目给我这个错误
无法找到程序集 'System.Diagnostics.DiagnosticSource, Version=4.0.2.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'。
堆栈跟踪
[SerializationException: Unable to find assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.2.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +153
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +336
System._AppDomain.CreateInstance(String assemblyName, String typeName) +0
System.Data.SqlClient.SqlDependency.CreateProcessDispatcher(_AppDomain masterDomain) +62
System.Data.SqlClient.SqlDependency.ObtainProcessDispatcher() +54
System.Data.SqlClient.SqlDependency.Start(String connectionString, String queue, Boolean useDefaults) +1095
System.Data.SqlClient.SqlDependency.Start(String connectionString) +13
HelperForYourHome.Admins.Hubs.Startup.Configuration(IAppBuilder app) in C:\Users\user\Documents\Projects\HelperForYourHome\HelperForYourHome.Admin\Hubs\Startup.cs:26
[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +150
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +101
Owin.Loader.<>c__DisplayClass12.<MakeDelegate>b__b(IAppBuilder builder) +66
Owin.Loader.<>c__DisplayClass1.<LoadImplementation>b__0(IAppBuilder builder) +123
Microsoft.Owin.Host.SystemWeb.<>c__DisplayClass2.<InitializeBlueprint>b__0(IAppBuilder builder) +71
Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(Action`1 startup) +462
Microsoft.Owin.Host.SystemWeb.OwinBuilder.Build(Action`1 startup) +40
Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint() +70
System.Threading.LazyInitializer.EnsureInitializedCore(T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) +115
Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(HttpApplication context) +106
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +536
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +173
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296
[HttpException (0x80004005): Exception has been thrown by the target of an invocation.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +10042604
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +95
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254
具体实现如下
欧文创业公司
[assembly: OwinStartup("Startup", typeof(Startup))]
namespace Admins.Hubs
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
var idProvider = new CustomUserIdProvider();
GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => idProvider);
app.MapSignalR();
SqlDependency.Start(Connection.ConnectionString);
var properties = new AppProperties(app.Properties);
CancellationToken token = properties.OnAppDisposing;
if (token != CancellationToken.None)
{
token.Register(() =>
{
SqlDependency.Stop(Connection.ConnectionString);
});
}
}
}
}
两个项目都在使用这个启动文件
这是我的消息处理程序class
public class MessagesRepository
{
readonly string _connString = Connection.ConnectionString;
public IEnumerable<NotificationMessage> GetAllMessages()
{
var messages = new List<NotificationMessage>();
using (var connection = new SqlConnection(_connString))
{
connection.Open();
using (var command = new SqlCommand(@"SELECT [ID],[Title],[Description],[MessageType],[NotificationType],[Icon],[IsAjaxMessage],[IsViewMessage],[IsRedirectMessage] FROM [General].[NotificationMessage]", connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read())
{
messages.Add(new NotificationMessage(reader));
}
}
}
return messages;
}
public virtual void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<Notification>();
context.Clients.All.GetNotifications();
}
}
}
一切正常。除了这只在 Web 项目中工作正常而不在管理项目中工作。
我也遇到了同样的问题。解决方案是在我所有的项目中升级 System.Net.Http。
这是 nuget 命令:Update-Package System.Net.Http -Version 4.3.3
这是 nuget 网页:System.Net.Http nuget page
希望它也能帮助解决您的问题。
亲切的问候