CodeFuller / stackoverflow

Repository with sample code to my answers on StackOverflow
MIT License
16 stars 8 forks source link

Fantasic example, but won't work for .NET Core 3+ #1

Open mcbridd opened 3 years ago

mcbridd commented 3 years ago

Hi,

I really appreciate people who put their time into producing code like this to help people like myself, but every time I find a useful example, it has been broken by changes to .NET Core versioning. In this case, the DefaultRazorPageFactoryProvider class and other Razor classes have now been changed to Internal in .NET Core 3.0, making them inaccessible.

Trying to figure out a way around it as I type.

Thanks, Darren

The-MAZZTer commented 3 years ago

Not sure what Razor has to do with this, but I can confirm this example doesn't seem to work in .NET Core 3.1. It looks like the hosted service is started before Configure is called.

The StartAsync method is async, but ASP.NET Core will wait around for us to start before doing anything else (boo!) but we can just spin off a new task and wait there.

    public Task StartAsync(CancellationToken cancellationToken) {
        Task.Run(this.GoAsync);
        return Task.CompletedTask;
    }

    private async Task GoAsync() {
        while (ServerAddresses == null) {
            await Task.Delay(250);
        }
                   /* same as before, but remove the return Task.CompletedTask */

This works for me, of course it's even more of a hack than before now.