dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.35k stars 9.99k forks source link

How to get the domain name of my site in ASP.NET Core 3.1 before the request in Program.cs #38891

Closed BalMahad closed 2 years ago

BalMahad commented 2 years ago

Is this a Bug or Feature request?: Feature request

I have my website hosted in the mywebsite folder of www.test.com and I visit https://www.test.com/mywebsite/home/about.

I have a requirement for implementing Multitenancy. In order to implement Multitenancy we need to get the domain name from baseurl and based on the domain name we need to change the connection string and other config items for each client.

How do I get the base url part in program.cs? The part that I am looking for is https://www.example.com

we are getting the config values from parameter store in AWS in program.cs as shown below host.ConfigureAppConfiguration(builder => builder.AddSystemsManager($"/env}"));

As our project is in .Net Core 3.1 we are using generic host public static IHostBuilder CreateHostBuilder(string[] args)

we are not able to get the base url from generic host.

But we are able to get the domain name using the web host as shown below in program.cs main method

public static void Main() { var host = BuildWebHost(null); host.Start(); var serverAddress = host.ServerFeatures.Get(); }

    private static IWebHost BuildWebHost(string[] args)
    {
        return WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
    }

how to get the domain name using generic host in program.cs main method?

Thanks.

davidfowl commented 2 years ago

By manually configuring it. There's no way ASP.NET Core can know what the domain name for your site is before a request happens. You are the only person with that information.

Tratcher commented 2 years ago

ServerFeatures are also available on the IApplicationBuilder in Startup: https://github.com/dotnet/aspnetcore/blob/69308dafeda51d5301396c88ba9136a6da97c7b6/src/Http/Http.Abstractions/src/IApplicationBuilder.cs#L24

rjgotten commented 2 years ago

I have a requirement for implementing Multitenancy. In order to implement Multitenancy we need to get the domain name from baseurl and based on the domain name we need to change the connection string and other config items for each client.

The Options framework has overloads that support late-bound factories that can take dependencies and most things are accessed via a per-request IOptionsSnapshot<>, iirc. So technically you should probably be able to use that to do what you want: just take a dependency on IHttpContextAccessor in your options factories and switch/case over the tenants; bind to different configuration sections (or providers) keyed on tenant ID; etc.

Btw. Properly adding an explicit per-tenant DI scope to ASP.NET Core is also ... interesting. I've resorted to doing it by patching the DefaultHttpContextFactory service registration so it takes in a specially doctored IServiceProvider that creates a tenant-level scope inbetween the root application scope and the per-request scope when the factory creates the per-request scope to stick into the HttpContext.RequestServices. As for registering services for that scope: it's kind of a hack as service descriptors have no way to provide custom life times. Instead I have a type IConfigureTenantServices registered in the root scope, which is executed to produce a second service collection, to add to the per-tenant scope using the underlying DI container (Autofac, Unity, etc.) which supports explicitly adding registration to child scopes / containers.

But then; you'll find some things will still completely break, because there are several key application features which just won't play ball with anything but root application scope. Notably: anything related to string localizers is an absolute pain.

Just putting that bit of knowledge out there...

ghost commented 2 years ago

Thank you for contacting us. Due to a lack of activity on this discussion issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.

This issue will be locked after 30 more days of inactivity. If you still wish to discuss this subject after then, please create a new issue!