Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

signalr

I' m working on a desktop application with .NET 6 and WinUI3. This application keeps tracking data changes in PLC and transfers real time data to my server and dashboard.

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.

I found Tutorial: SignalR Self-Host but it's not for .NET Framework 4.x. Can I still host a SignalR server in my desktop application with Asp.Net Core SignalR?


I'm a non-native English speaker. There might be some grammatical mistakes in my description and I'm sorry for that.

like image 930
hangriver Avatar asked Dec 16 '25 21:12

hangriver


1 Answers

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");
        });
    }
}
like image 104
Athanasios Kataras Avatar answered Dec 19 '25 11:12

Athanasios Kataras



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!