【问题标题】:cannot convert from 'TableDependency.SqlClient.Base.Enums.DmlTriggerType' to 'TableDependency.SqlClient.Base.Abstracts.ITableDependencyFilter'无法从“TableDependency.SqlClient.Base.Enums.DmlTriggerType”转换为“TableDependency.SqlClient.Base.Abstracts.ITableDependencyFilter”
【发布时间】:2024-01-08 06:41:01
【问题描述】:

我正在尝试遵循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
        }

【问题讨论】:

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


    【解决方案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 允许您向连接的客户端发送消息。它具有许多与您在 Hub 中时相同的与客户通信的功能。

    为了获得HubContext 的实例,您需要通过在构造函数中指定您想要IHubContext&lt;T&gt; 来使用依赖注入。 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

    【讨论】: