无法从“ TableDependency.SqlClient.Base.Enums.DmlTriggerType”转换为“ TableDependency.SqlClient.Base.Abstracts.ITableDependencyFilter”

时间:2019-08-26 18:20:22

标签: asp.net-core .net-core signalr asp.net-core-2.2 sqldependency

我正在尝试遵循this教程,但遇到此错误

  

参数6:无法从   'TableDependency.SqlClient.Base.Enums.DmlTriggerType'到   'TableDependency.SqlClient.Base.Abstracts.ITableDependencyFilter'   (CS1503)

此外,在同一教程中,作者在这样的启动文件中使用了Hubcontext

services.AddScoped<IHubContext<NonProductionHub>, HubContext<NonProductionHub>>();

我不确定它是否正确,因为我在HubContext而不是IHubContext上遇到以下错误

  

找不到类型或名称空间名称'HubContext <>'(您是   缺少using指令或程序集引用?)

public class InventoryDatabaseSubscription : IDatabaseSubscription
        {
            private bool disposedValue = false;
            private readonly IInventoryRepository _repository;
            private readonly IHubContext<NonProductionHub> _hubContext;
            private SqlTableDependency<Apps> _tableDependency;

            public InventoryDatabaseSubscription(IInventoryRepository repository, IHubContext<NonProductionHub> hubContext)
            {
                _repository = repository;
                _hubContext = hubContext;            
            }

            public void Configure(string DefaultConnection)
            {
                _tableDependency = new SqlTableDependency<Apps>(DefaultConnection, null, null, null, null, DmlTriggerType.All);
                _tableDependency.OnChanged += Changed;
                _tableDependency.OnError += TableDependency_OnError;
                _tableDependency.Start();

                Console.WriteLine("Waiting for receiving notifications...");
            }

            private void TableDependency_OnError(object sender, ErrorEventArgs e)
            {
                Console.WriteLine($"SqlTableDependency error: {e.Error.Message}");
            }

            private void Changed(object sender, RecordChangedEventArgs<Apps> e)
            {
                if (e.ChangeType != ChangeType.None)
                {
                    // TODO: manage the changed entity
                    var changedEntity = e.Entity;
                    _hubContext.Clients.All.SendAsync("UpdateCatalog", _repository.Apps);
                }
            }

            #region IDisposable

            ~InventoryDatabaseSubscription()
            {
                Dispose(false);
            }

            protected virtual void Dispose(bool disposing)
            {
                if (!disposedValue)
                {
                    if (disposing)
                    {
                        _tableDependency.Stop();
                    }

                    disposedValue = true;
                }
            }

            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }

            #endregion
        }

1 个答案:

答案 0 :(得分:1)

  

参数6:无法从“ TableDependency.SqlClient.Base.Enums.DmlTriggerType”转换为“ TableDependency.SqlClient.Base.Abstracts.ITableDependencyFilter”(CS1503)

从错误中,您可以转到SqlTableDependency方法的定义以检查包含的参数

public SqlTableDependency(string connectionString, string tableName = null, string schemaName = null, 
                          IModelToTableMapper<T> mapper = null, IUpdateOfModel<T> updateOf = null, 
                          ITableDependencyFilter filter = null, DmlTriggerType notifyOn = DmlTriggerType.All, 
                          bool executeUserPermissionCheck = true, bool includeOldValues = false);

DmlTriggerType.All应该是第七而不是第六,并且第六参数的值为null,如下所示更改代码:

_tableDependency = new SqlTableDependency<Apps>(DefaultConnection, null, null, null, null, null, DmlTriggerType.All);
  

找不到类型或名称空间名称'HubContext <>'(您是否缺少using指令或程序集引用?)

HubContext允许您将消息发送到连接的客户端。与您在集线器内部时一样,它具有许多与客户端通信的功能。

为了获取HubContext的实例,需要通过在构造函数中指定要使用IHubContext<T>来使用依赖项注入。其中T是您的中心。请参考以下示例:

public class HomeController : Controller
{ 
   private readonly IHubContext<NotificationHub> _hubContext;

   public HomeController(IHubContext<NotificationHub> hubContext)
   {
      _hubContext = hubContext;
   }
}

裁判:https://docs.microsoft.com/en-us/aspnet/core/signalr/hubcontext?view=aspnetcore-2.2