Shutting down an RO server when using Microsoft.Extensions.Hosting

Using VS2022, C# 10, RO 10.0.0.1533, .Net 6, WPF, code first

Since there are apparently no examples or documentation of using RO with Microsoft.Extensions.Hosting (and if there are, please point me to it), can someone suggest how I’m supposed to shutdown an RO server when using WPF?

I’m constructing/starting the server like this (along with various other things I’ve tried):

            hostBuilder = ConstructHost();

            host = hostBuilder.Build();
            host.StartAsync();

and then I start WPF/my window, and all appears well at that point. The server starts, I can communicate with it, the window works, etc.

My problem comes when shutting down the server. When doing so, the Debug console shows me this:

Microsoft.Hosting.Lifetime: Information: Waiting for the host to be disposed. Ensure all ‘IHost’ instances are wrapped in ‘using’ blocks.

I tried using await host.StopAsync() but that never returns, so obviously that’s not the right way.

I have tried a number of things, and some serious Googling about how to deal with this, nothing worked and the examples on the internet don’t seem to match at all. The examples I’ve found are showing RO should be implementing IHostedService somewhere and I could just create a StopAsync and I should be fine. The problem is that I don’t seem to have a way/location to do that, services from ConfigureServices doesn’t seem to have AddHostedService

At this point, given no example or documentation, I’m not sure if I’m starting it wrong, trying to stop it wrong or just have a missing piece somewhere.

Hello

These are the samples:
WinForns: WinFormsApp1.zip (4.5 KB)

WPF: WpfApp1.zip (3.7 KB)

For WinForms the main point is to use the IHost to manage application lifetime, including the IAppServerEngine service (the core of a RO SDK server running in your application).

For WPF application IHost is started/stopped in the Application event handlers.

These samples should give you a good start with the UI + RO SDK server mix.

Note: There is also an old-fashioned approach where all RO SDK components are created manually:
WpfApp1.zip (3.6 KB)
With this approach you will have to explicitly create all Remoting SDK infrastructure components.

Hope that helps

I appreciate the samples, thanks, the problem is that while the Winform example works fine, the WPF sample does not shutdown at all, which I guess explains why my attempts to make an app that shuts down did not work.

I open the project, run it, click the Press Me button on the form and the window closes immediately. Visual Studio, however, stays in debug and the application is still running. Looking at my debug output (included below) I’m wondering if the problem stems from the “Press Ctrl+C to shut down.” message I get in the the Output tab when I run. The thread exit messages appear every ~30 seconds or so. I’ve let it sit for > 5 minutes to no avail.

When I first ran it, I was getting a warning from VS that I did not have .net5 installed (though it ran anyway), so I installed it via the VS installer and tried again, with the same result. I can confirm the app does not work outside of VS either (not that I expected it to), I have to kill the process.

Any suggestions on where to look here? I’m guessing some strange bug, in either RO or Hosting, that you’re not hitting for some reason.

Packages: DependencyInjection 6.0.0, Hosting: 6.0.1. RO 10.0.0.1533

Then use this approach with directly created RO components:
WpfApp1.zip (3.6 KB)

The issue is that Hosing packages were never designed to be used in a GUI-based application. considering that Microsoft.Extensions.Hosting package itself does not provide native support for WinForms or WPF applications.

Suggestion for anyone following after me trying to make this work. Using RO with Microsoft.Extensions.Hosting and WPF is viable, and not especially difficult, given the right add on packages. I can suggest both:

Dapplo.Microsoft.Extensions.Hosting.Wpf

and

WPF.Extensions.Hosting

both from NuGet. I ended up using WPF.Extentions.Hosting, though it required some minor recoding of my base Program class, due to a different usage scheme from the base Microsoft.Extensions.Hosting. I originally started with Dapplo, but was unable to make it work with IpSuperHttpServerChannel for some reason that I couldn’t figure out, and ended up with WPF.Extensions.Hosting. In the end I ended up with a single Program class that let’s me have an application be Console or WPF and toggle between them at load time (eventually via command line).

1 Like