简体   繁体   English

如何在 .NET5/6 桌面应用程序中自托管 SignalR 服务器?

[英]How to Self-Host SignalR server in .NET5/6 desktop application?

I' m working on a desktop application with .NET 6 and WinUI3.我正在使用 .NET 6 和 WinUI3 开发桌面应用程序。 This application keeps tracking data changes in PLC and transfers real time data to my server and dashboard.该应用程序持续跟踪 PLC 中的数据变化,并将实时数据传输到我的服务器和仪表板。

My server is cloud based.我的服务器是基于云的。 Using the server as a message hub to transmit the data will cause unnecessary delay and performance occupation.使用服务器作为消息中枢来传输数据会造成不必要的延迟和性能占用。 That's why I want to host a signalR server in my desktop application so that it could work as a lightweight web server.这就是为什么我想在我的桌面应用程序中托管 signalR 服务器,以便它可以作为轻量级 web 服务器工作。

I found Tutorial: SignalR Self-Host but it's not for .NET Framework 4.x.我找到了教程:SignalR Self-Host但它不适用于 .NET Framework 4.x。 Can I still host a SignalR server in my desktop application with Asp.Net Core SignalR?我仍然可以使用 Asp.Net Core SignalR 在我的桌面应用程序中托管 SignalR 服务器吗?


I'm a non-native English speaker.我是一个非英语母语人士。 There might be some grammatical mistakes in my description and I'm sorry for that.我的描述中可能存在一些语法错误,对此我深表歉意。

You can most probably leverage the background services offered in all net core applications.您很可能可以利用所有网络核心应用程序中提供的后台服务。

signalr/background-services 信号器/后台服务

Setting it up, can be as simple as this:设置它,可以像这样简单:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSignalR();
        services.AddHostedService<Worker>();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<ClockHub>("/hubs/clock");
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM