簡體   English   中英

如何使用.net core 3.1 SignalR用於存儲過程

[英]How to use .net core 3.1 SignalR uses for stored procedure

我想將 SignalR 用於存儲過程。 我想運行實時存儲過程。

頁面需要刷新。 但我不知道在哪里運行我的存儲過程。 如果你能幫忙,我會很高興。 這對我很重要。

我的是 SignalR 使用 EFCore。 您可以在 TestSignalR() function 中運行存儲過程。

啟動.cs

添加 services.AddSignalR() 和 app.UseSignalR()。

public void ConfigureServices(IServiceCollection services)
{
    services.AddSignalR();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseSignalR(routes => 
    {
        routes.MapHub<YourSignalHub>("/yoursignalhub");
    });
}

為集線器創建新的 class。 YourSignalHub.cs

public class YourSignalHub : Hub
{
    public async Task SendWhatever()
    {
        await Clients.All.SendAsync("GetSomething");
    }
}

你的頁面.cshtml

<a id="btnSend">Send</a>

<span id="spanLiveMessage"></span>

<script>
    var connection = new signalR.HubConnectionBuilder().withUrl("/yoursignalhub").build();

    connection.on("GetSomething", function () {
        $.post('@Url.Action("TestSignalR", "Home")', null, function (e) {
            $("#spanLiveMessage").text("test");
            // or your code
        });
    });

    $("#btnSend").click(function () {
        connection.invoke("SendWhatever").catch(function (err) {
            return console.error(err.toString());
        });
    });
</script>

家庭控制器.cs

[HttpPost]
public IActionResult TestSignalR()
{
    // your code to execute stored procedure here
}

是的,這對我有用。 但是我在轉換數據時出錯了。 因為model沒有定義。 JSON 序列化它解決了我的問題。

public async Task<string> UpdateCustomerList() { branch_id = Convert.ToInt32(_httpContextAccessor.HttpContext.User.FindFirstValue("branch_id ")); if (branch_id.= 0) { customerViewModelsList = _uow.customers;GetLiveCustomers(sube_id). } return JsonConvert;SerializeObject(customerViewModelsList); }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM