Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom IServiceCollection in build method #44435

Closed
ti24horas opened this issue May 2, 2017 · 14 comments
Closed

Allow custom IServiceCollection in build method #44435

ti24horas opened this issue May 2, 2017 · 14 comments
Labels
area-Extensions-Hosting untriaged New issue has not been triaged by the area owner

Comments

@ti24horas
Copy link

Is that possible to pass a custom IServiceCollection instead of using the internal IServiceCollection created in Build method?

My case is that the WebHostBuilder is just part of a bigger application (which contains part of code running as a service, for example)

Because the services are registered by an external IServiceCollection, it would be impossible to utilize the existing IServiceProvider without duplicating registrations (or delegating them to existing IServiceProvider).

@davidfowl
Copy link
Member

This isn't possible today. The WebHostBuilder is the root of the DI universe and you can't replace the built in IServiceCollection.

@ti24horas
Copy link
Author

@davidfowl I wonder if the IServiceCollection could be passed as a parameter in "Build" method of WebHostBuilder (as an optional parameter, btw), I think it would have a slight impact on what's running today.

I just don't want to mimic the WebHost class just to be able to pass an existing service configuration.

kaztechSolutions referenced this issue in kaztechSolutions/Hosting Dec 27, 2017
Allow to pass an existing IServiceCollection instance to WebHostBuilder which should be used instead of creating a new one inside WebHostBuilder. Could be a solution to issue #1060.
@aspnet-hello aspnet-hello transferred this issue from aspnet/Hosting Dec 18, 2018
@lanwin
Copy link

lanwin commented Jul 1, 2019

I would also love to have such an option.

We also haven an existing bootstrapping project where we already create an IServiceCollection and currently its pretty hard to use the WebHostBuilder in that scenario. I made some hacks with IProviderFactory and stuff and it works. But it feels kinda hacky and official support for such a scenario would be very helpful.

@dmlarionov
Copy link

dmlarionov commented Jul 25, 2019

I would also love to have such an option. I want to use ILogger a few steps before building WebHostBuilder.

It's quite logical, in my opinion, to configure IServiceCollection with ILogger, build services, get ILogger from it, execute initial steps (perhaps use ILogger instance), then pass that IServiceCollection or services or appendable DI configuration in any form to WebHostBuilder, knowing that services configured earlier in pipeline become configured in WebHost.

@Tratcher
Copy link
Member

Tratcher commented Nov 9, 2020

Moving to runtime. We plan to obsolete WebHostBuilder so any feature work here would have to go into the generic HostBuilder.

@Tratcher Tratcher transferred this issue from dotnet/aspnetcore Nov 9, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.ServiceProcess untriaged New issue has not been triaged by the area owner labels Nov 9, 2020
@ghost
Copy link

ghost commented Nov 9, 2020

Tagging subscribers to this area: @eerhardt, @maryamariyan
See info in area-owners.md if you want to be subscribed.


Issue meta data

Issue content: Is that possible to pass a custom IServiceCollection instead of using the internal IServiceCollection created in Build method?

My case is that the WebHostBuilder is just part of a bigger application (which contains part of code running as a service, for example)

Because the services are registered by an external IServiceCollection, it would be impossible to utilize the existing IServiceProvider without duplicating registrations (or delegating them to existing IServiceProvider).

Issue author: ti24horas
Assignees: -
Milestone: -

@davidfowl
Copy link
Member

This should be closed, I'm not exactly sure what a custom Build method would do here. We have an IServiceProviderFactory<TContainer> for extensibility in the host which satisfies this need.

@lanwin
Copy link

lanwin commented Feb 4, 2021

This should be closed, I'm not exactly sure what a custom Build method would do here. We have an IServiceProviderFactory<TContainer> for extensibility in the host which satisfies this need.

I didnt have tried the HostBuilder yet, but the problem here was not the extensibility from my point of view.

The WebHostBuilder always builds its own ServiceCollection and allows other only to hook in. But what about scenarios where you already have another application root ServiceCollection and only want to add a webhost to it?

@davidfowl
Copy link
Member

HostBuilder has a similar problem but that's by design as a host is responsible for creating and managing the DI container and hosted services within that container. It sounds like you're looking for something else.

What experience are you looking for?

@lanwin
Copy link

lanwin commented Feb 4, 2021

We have an company intern boostrap system for services/docker and co, witch builds upon everything we need for all off our services. This works also with ServiceCollection but it creates one already at start time. We want to use HTTP/ASP.NET as opt in feature to it. So manly wee need the setup part from WebHostBuilder to work against and existing ServiceCollection.

I mad it work by using forwarding IServiceProvider. But its complicated and feels messy while all it would nee is that WebHostBuilder would add a Ctor witch accepts an ServiceCollection.

@davidfowl
Copy link
Member

Maybe you should target the the IHostBuilder directly instead of the IServiceCollection? Can you show what you're doing right now (a code sample would be best)?

FWIW, I think it would be possible to take an initial IServiceCollection, but taking a baked IServiceProvider wouldn't be possible.

@lanwin
Copy link

lanwin commented Feb 4, 2021

I will try to revisit that and build an example. But it could take a little while right now.

@davidfowl
Copy link
Member

This issue is 4 years old, I got time 😆

@maryamariyan
Copy link
Member

should be closed, I'm not exactly sure what a custom Build method would do here. We have an IServiceProviderFactory for extensibility in the host which satisfies this need.

@ghost ghost locked as resolved and limited conversation to collaborators Apr 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Hosting untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

7 participants