Skip to content

Issue in converting Microsoft.AspNetCore.WebHost to Microsoft.Extensions.Hosting.Host #64446

@WenningQiu

Description

@WenningQiu

My application has code like below to set up an ASP.NET Core host using WebHost (.NET 8.0):

public class WCFServiceHost
{
    private void SetupHttpWebHost(Uri[] baseAddresses)
    {
        var builder = Microsoft.AspNetCore.WebHost.CreateDefaultBuilder()
            .ConfigureServices(x => x.AddSingleton(this))
            .UseStartup<Startup>();

       builder.Build().RunAsync();
    }
}

internal class Startup
{
        public Startup(WCFServiceHost wcfServiceHost)
        {
        }
}

Since WebHost becomes obsolete in .NET 10, I try to convert it to using Host with the code like below:

public class WCFServiceHost
{
    private void SetupHttpWebHost(Uri[] baseAddresses)
    {
        var builder = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
            .ConfigureServices(x => x.AddSingleton(this))
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });

        builder.Build().RunAsync();
    }
}

However, the application is getting
System.InvalidOperationException : Unable to resolve service for type 'WCFServiceHost' while attempting to activate 'Startup'. error.

I appreciate if someone can provide guidance about the right way of using Microsoft.Extensions.Hosting.Host.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions