我在ASP.NET Core 8.0项目中使用Dapper和N层体系 struct .我的项目使用SignalR工作,但当我添加SqlDependency
时,我得到一个错误
无法连接到Web服务器HTTPS
轮毂侧:
public class AppHub : Hub
{
private readonly IExpenseService _expenseService;
public AppHub(IExpenseService expenseService)
{
_expenseService = expenseService;
}
public async Task SendExpense()
{
var value = _expenseService.itemListExpense();
await Clients.All.SendAsync("ReceiveExpense", value);
}
}
表依赖关系:
private readonly SqlTableDependency<ResultExpenseDto> _tableDependency;
private readonly AppHub _appHub;
public ExpenseDependency(AppHub appHub)
{
string connectionString = "Data Source=ADAPC\\SQLEXPRESS;Initial Catalog=DB_WPMS;Integrated Security=True;TrustServerCertificate=True;";
_appHub = appHub;
_tableDependency = new SqlTableDependency<ResultExpenseDto>(connectionString);
_tableDependency.OnChanged += _tableDependency_OnChanged;
_tableDependency.OnError += _tableDependency_OnError;
}
public void Start()
{
_tableDependency.Start();
}
private void _tableDependency_OnError(object sender, TableDependency.SqlClient.Base.EventArgs.ErrorEventArgs e)
{
throw new NotImplementedException();
}
private void _tableDependency_OnChanged(object sender, TableDependency.SqlClient.Base.EventArgs.RecordChangedEventArgs<ResultExpenseDto> e)
{
if (e.ChangeType != TableDependency.SqlClient.Base.Enums.ChangeType.None)
{
_appHub.SendExpense();
}
}
ApplicationBuilder扩展:
public static class ApplicationBuilderExtensions
{
public static void UseTableDependency(this IApplicationBuilder applicationBuilder)
{
var serviceProvider = applicationBuilder.ApplicationServices;
var service = serviceProvider.GetService<ExpenseDependency>();
service.Start();
}
}
Program.cs
个文件:Program.cs
个文件:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", builder =>
{
builder.AllowAnyHeader()
.AllowAnyMethod()
.SetIsOriginAllowed((host) => true)
.AllowCredentials();
});
});
builder.Services.AddHttpClient();
builder.Services.AddControllersWithViews();
builder.Services.AddSignalR();
builder.Services.AddSingleton<AppHub>();
builder.Services.AddSingleton<ExpenseDependency>();
builder.Services.AddDbContext<Context>();
builder.Services.AddIdentity<AppUser, AppRole>(options =>
{
options.User.RequireUniqueEmail = true;
}).AddEntityFrameworkStores<Context>().AddErrorDescriber<CustomIdentityValidator>().AddDefaultTokenProviders();
builder.Services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Login/Index";
});
builder.Services.AddSession();
builder.Services.AddDistributedMemoryCache();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseSession();
app.UseCors("CorsPolicy");
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapHub<AppHub>("/appHub");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.UseTableDependency();
app.Run();
当我注释掉第app.UseTableDependency();
行时,项目运行,但自然不使用表依赖关系.
错误是什么?我请求你们的支持.