dotnet / AspNetCore.Docs

Documentation for ASP.NET Core
https://docs.microsoft.com/aspnet/core
Creative Commons Attribution 4.0 International
12.58k stars 25.29k forks source link

How to specify "hosting startup assemblies" via code using the "minimal hosting model"? #30494

Open jhwrage opened 1 year ago

jhwrage commented 1 year ago

Please describe if and how you can set hosting startup assemblies in the code when using the new "minimal hosting model" of ASP.NET Core 6.0 and higher.

Or give a hint to https://github.com/dotnet/aspnetcore/issues/38765


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Rick-Anderson commented 1 year ago

Copy/paste into ChatGPT produces:

Here's how you can set up hosting startup assemblies in the code using ASP.NET Core 6.0's minimal hosting model:

Create a Hosting Startup Assembly:

Define a class (e.g., MyHostingStartup) that implements the IHostingStartup interface. Implement the Configure method to configure services or the app.

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

public class MyHostingStartup : IHostingStartup
{
    public void Configure(IWebHostBuilder builder)
    {
        builder.ConfigureServices(services =>
        {
            services.AddSingleton<IService, MyService>();
        });
    }
}

Register the Hosting Startup Assembly:

In the minimal hosting model, you'd typically use WebApplication.CreateBuilder to get an instance of WebApplicationBuilder. The hosting startup assembly can be registered using the UseSetting method with the WebHostDefaults.HostingStartupAssembliesKey key.

var builder = WebApplication.CreateBuilder(args);
builder.UseSetting(WebHostDefaults.HostingStartupAssembliesKey, "NameOfYourAssembly");

Additional Configuration (if needed):

If your hosting startup needs environment-specific services or configuration, you can make use of the builder's Environment property or other available properties/methods. Finalize the App Creation:

Once you've configured the builder, you can continue to define your app routes and finally build the app.

var app = builder.Build();
// Define middleware, endpoints, etc.
app.Run();

Enabling or Disabling Hosting Startup Assemblies via Environment Variables:

Sometimes, you might want to enable or disable certain hosting startup assemblies during runtime or based on environments. You can leverage environment variables to achieve this: ASPNETCORE_HOSTINGSTARTUPASSEMBLIES: Contains a semicolon-delimited list of assembly names to load on startup. ASPNETCORE_SUPPRESSHOSTINGSTARTUP: Set to true to suppress all hosting startup assemblies. Remember, when you're working with hosting startup assemblies, be cautious. Loading unnecessary assemblies or configurations can affect the performance and behavior of your application. Always test thoroughly when integrating hosting startup assemblies into your application.

jhwrage commented 1 year ago

This is (once again) a nice answer from ChatGPT, but it is (once again) simply wrong:

builder.UseSetting(WebHostDefaults.HostingStartupAssembliesKey, "NameOfYourAssembly");

WebApplicationBuilder has no method "UseSetting".

This is a workaround (copied from stackoverflow or this issue):

Environment.SetEnvironmentVariable(
    $"ASPNETCORE_{WebHostDefaults.HostingStartupAssembliesKey}", 
    "[ASSEMBLY_HERE]");

var builder = WebApplication.CreateBuilder(args);

I just want to suggest that a corresponding note be included in the documentation. This can save a lot of time for users who use the minimal hosting model.