我在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();行时,项目运行,但自然不使用表依赖关系.

错误是什么?我请求你们的支持.

推荐答案

由于缺少您定制的SqlTableDependency代码,我们很难复制.

但你的ExpenseDependency级有一个明显的错误.不应该使用Private readonly AppHub _appHub;,但应该使用private readonly IHubContext<AppHub> _appHub;.

并且您使用UseTableDependency来启动SqlDependency,这是错误的方式.

这是最适合你的做法. Implementing SignalR in ASP.NET Core Web App (MVC) for real-time database updates

我已经测试了它,它工作得很好,我移动启动方法如下.

enter image description here

我们可以像下面这样使用它.我们不需要使用您的定制中间件UseTableDependency.

enter image description here

Javascript相关问答推荐

如何在dataTables PDF输出中正确渲染字形?

React Native平面列表自动滚动

从实时数据库(Firebase)上的子类别读取数据

在观察框架中搜索CSV数据

jQuery s data()[storage object]在vanilla JavaScript中?'

如何在 cypress 中使用静态嵌套循环

JS,当你点击卡片下方的绿色空间,而它是在它的背后转动时,

还原器未正确更新状态

为什么useState触发具有相同值的呈现

制作钢琴模拟器,并且在控制台中不会执行或显示该脚本

使用原型判断对象是否为类的实例

环境值在.js文件/Next.js中不起作用

在Reaction中的handleSubmit按钮内,useSelector值仍然为空

如何在JAVASCRIPT中合并两组对象并返回一些键

try 使用PM2在AWS ubuntu服务器上运行 node 进程时出错

如何在FastAPI中为通过file:/URL加载的本地HTML文件启用CORS?

如何组合Multer上传?

如何在Reaction中清除输入字段

需要RTK-在ReactJS中查询多个组件的Mutations 数据

需要刷新以查看Mern堆栈应用程序中的更改