带有 Dapper 的 ASP.NET Core Web Api(SignalR-SqlTableDependency):无法连接到 Web 服务器 https

问题描述 投票:0回答:1

我在 ASP.NET Core 8.0 项目中使用 Dapper 和 N 层架构。我的项目使用 SignalR,但是当我添加

SqlDependency
时,出现错误

无法连接到网络服务器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
文件:

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();
时,项目会运行,但自然不会使用表依赖项。

错误是什么?我请求您的支持。

javascript signalr asp.net-core-webapi dapper sqldependency
1个回答
0
投票

由于缺少您定制的SqlTableDependency代码,我们很难重现。

但是你的

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

并且您使用

UseTableDependency
来启动SqlDependency,这是错误的方式。

这是最适合您的做法。 在 ASP.NET Core Web App (MVC) 中实现 SignalR 以进行实时数据库更新

我已经测试过它,效果很好,我将启动方法移动如下。

我们可以像下面这样使用它。我们不需要使用您的自定义中间件

UseTableDependency

© www.soinside.com 2019 - 2024. All rights reserved.