I'm trying to follow this tutorial, but getting this error
Argument 6: cannot convert from 'TableDependency.SqlClient.Base.Enums.DmlTriggerType' to 'TableDependency.SqlClient.Base.Abstracts.ITableDependencyFilter' (CS1503)
Also, in the same tutorial author used Hubcontext in startup file like this
services.AddScoped<IHubContext<NonProductionHub>, HubContext<NonProductionHub>>();
I'm not sure to whether its correct or not because I'm getting the following error on HubContext
and not on IHubContext
The type or namespace name 'HubContext<>' could not be found (are you missing a using directive or an assembly reference?)
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
}
Argument 6: cannot convert from 'TableDependency.SqlClient.Base.Enums.DmlTriggerType' to 'TableDependency.SqlClient.Base.Abstracts.ITableDependencyFilter' (CS1503)
From the error , you could go to the definition of SqlTableDependency method to check the arguments contained
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);
The value DmlTriggerType.All
should be the seventh instead of sixth, and the value of the sixth parameter is null , change the code like below :
_tableDependency = new SqlTableDependency<Apps>(DefaultConnection, null, null, null, null, null, DmlTriggerType.All);
The type or namespace name 'HubContext<>' could not be found (are you missing a using directive or an assembly reference?)
The HubContext
allows you to send messages to your connected clients. It has many of the same features to communicate with clients as when you are inside of a Hub.
In order to get an instance of the HubContext
, you need to be using dependency injection by specifying you want an IHubContext<T>
in the constructor. Where T
is your Hub. Refer to the following example :
public class HomeController : Controller
{
private readonly IHubContext<NotificationHub> _hubContext;
public HomeController(IHubContext<NotificationHub> hubContext)
{
_hubContext = hubContext;
}
}
Referece :https://learn.microsoft.com/en-us/aspnet/core/signalr/hubcontext?view=aspnetcore-2.2