简体   繁体   中英

How to self host SignalR hub in c# Winforms?

There are a lot of tutorials out there for setting up SignalR selfhost in a console application or a background service.

eg https://docs.microsoft.com/en-us/aspnet/signalr/overview/deployment/tutorial-signalr-self-host

I am having a lot of trouble figuring out how to do it from winforms? there is a similar question here

how to setup a C# winforms application to host SignalR Hubs?

but they have answered with a console app... so here is the code that works in the console:

        string url = @"http://127.0.0.1:8585/";

        using (WebApp.Start<Startup>(url))
        {
            Console.WriteLine(string.Format("Server running at {0}", url));
            Console.ReadLine();
        }

I know I might need to start a separate thread but how would it keep running the server? I have tried using the same on load.

using block means that there will be executed Dispose method on instance created by WebApp.Start<Startup>(url) statement.

in console app using block is not finished until user press enter ( Console.ReadLine(); ).

in wpf using block finishes without delay. try WebApp.Start<Startup>(url); (and stop it when appropriate like application Exit)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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