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

Make Configuration.AddServices public #54

Open
irontoby opened this issue Aug 16, 2019 · 8 comments
Open

Make Configuration.AddServices public #54

irontoby opened this issue Aug 16, 2019 · 8 comments
Assignees
Labels
Enhancement 🔨 Improvement of existing features

Comments

@irontoby
Copy link

We have an existing ASP.NET (not Core) app which is using Unity container for DI. However we would like to add some new Microsoft services such as health checks which require an IServiceCollection to configure.

It would be very helpful if I could create a ServiceCollection, add the new Microsoft services, and then populate an existing Unity container with those services. (This seems to be the same request as #23 and/or #24 which were closed by the submitter w/o comment).

It looks like IUnityContainer AddServices(this IUnityContainer container, IServiceCollection services) in Configuration.cs does exactly this, but it's currently internal. Is there any reason to not make it public?

I can currently create a ServiceProviderFactory then call CreateBuilder but that does not allow me to re-use the Unity container I already have.

@ENikS
Copy link
Contributor

ENikS commented Aug 16, 2019 via email

@irontoby
Copy link
Author

For example, to set up Microsoft health checks it is necessary to call their extension method AddHealthChecks(this IServiceCollection services) (source code here). So I need an IServiceCollection interface to pass to that method. Their method in turn uses internal implementations of HealthCheckService and IHostedService so I cannot set those up directly.

I agree if I could just pass the container to that method it would be best, but I don't see any other way to do that. Is there a better way I'm missing?

@ENikS
Copy link
Contributor

ENikS commented Aug 16, 2019 via email

@cduivis
Copy link
Contributor

cduivis commented Jan 6, 2020

I'm facing the same issue, you've written an really nice library, with a really good structure.
We currently have our own provider factory which partly makes use of this library, but was left on the 2.x version for various reasons.

Now that I'm upgrading from the 2.x to the version 5.x of this library, I'm noticing that I basically need to copy paste all the internal logic, just because I want to make use of the AddServices extension method of the Configuration class.

For which I would also need to copy paste the InjectionTransientLifetimeManager because it's class is set to internal (for which I created a PR btw : #72).

@ENikS
Copy link
Contributor

ENikS commented Jan 6, 2020

Could you provide me with a sample of how you want it to be used? Perhaps couple of different scenarios?

I'd like to make sure I understand it correctly.

@cduivis
Copy link
Contributor

cduivis commented Jan 7, 2020

Our scenario has similar behavior as this packages use of Configuration.AddServices. But we are creating dependency injection for a multi-tenant environment, which requires us to write our own provider factory and provider. But for the normal defined dependencies we want to pass them along to Unity.
Basically these which are typically defined in the Startup.cs:

services
	.AddScoped<IMyDependency, MyDependency>()
	.AddTransient<IOperationTransient, Operation>()
	.AddScoped<IOperationScoped, Operation>()
	.AddSingleton<IOperationSingleton, Operation>()
	.AddSingleton<IOperationSingletonInstance>(new Operation(Guid.Empty));

(As far as I can see that's the same way this package is using the Configuration.AddServices?)

And because you've already written that "plumbing" for this perfectly it would be a waste to rewrite the exact same behavior.

@ENikS
Copy link
Contributor

ENikS commented Jan 9, 2020

What stops you from doing this?

public void ConfigureServices(IServiceCollection services)
{
    services
	.AddScoped<IMyDependency, MyDependency>()
	.AddTransient<IOperationTransient, Operation>()
	.AddScoped<IOperationScoped, Operation>()
	.AddSingleton<IOperationSingleton, Operation>()
	.AddSingleton<IOperationSingletonInstance>(new Operation(Guid.Empty));
}

Unity supports normal initialization provided in Startup.cs

@Patrickkk
Copy link

What stops you from doing this?

public void ConfigureServices(IServiceCollection services)
{
    services
	.AddScoped<IMyDependency, MyDependency>()
	.AddTransient<IOperationTransient, Operation>()
	.AddScoped<IOperationScoped, Operation>()
	.AddSingleton<IOperationSingleton, Operation>()
	.AddSingleton<IOperationSingletonInstance>(new Operation(Guid.Empty));
}

Unity supports normal initialization provided in Startup.cs

When using this for non web applications such as console applications this would be usefull :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement 🔨 Improvement of existing features
Projects
None yet
Development

No branches or pull requests

4 participants