asp.netmodel-view-controllersignalrsqldependency

Unable to find assembly System.Diagnostics.DiagnosticSource when using SqlDependency with SignalR in Multiple Projects


I have two asp.net MVC Projects in one solution. AdminPanel and Web. I need to show notifications using signalR and whenever a notification table is updated. I am using SqlDependency with signalR for this. The Web Project runs fine but the Admin Project gives me this Error

Unable to find assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.2.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

Stack Trace

[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

Here is the implimentation

Owin Startup

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

This one Startup File is being used by both projects

this is my messagehandler 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();
        }
    }
}

This all is working fine. Except this is only working Fine in Web Project and Not in Admin Project.


Solution

  • I've got the same issue. The solution was to upgrade System.Net.Http in all my projects.

    This is the nuget command : Update-Package System.Net.Http -Version 4.3.3 And this is the nuget web page : System.Net.Http nuget page

    Hope it will help to fix your problem also.

    Kind regards