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

Add DI compatibility test #50029

Open
aarrgard opened this issue Nov 22, 2018 · 8 comments
Open

Add DI compatibility test #50029

aarrgard opened this issue Nov 22, 2018 · 8 comments

Comments

@aarrgard
Copy link

aarrgard commented Nov 22, 2018

We are currently using Unity in some of our projects and want to use it with asp.net core. To enable this feature we use the "Unity.Microsoft.DependencyInjection - 2.1.1". We also register some types in the default ServiceCollection provided here that then resolves types from the Unity registrations. There seems to be an issue how unity resolves object by using lambda functions. Since all the DI tests runs successfully using the Unity bridge perhaps the "Microsoft.Extensions.DependencyInjection.Specification.Tests" needs to address this.

The issue is related to scoped containers.

  1. Create and register a transient type that takes a scoped type as parameter.
  2. Create service provider
  3. Create scoped provider
  4. Make sure that the scoped type comes from the child container when getting the transient type.

This works in Unity if you register the type with

serviceCollection.AddTransient<ITransientService, TransientService>()

but not if you register it with

serviceCollection.AddTransient<ITransientService>(sp=>new TransientService(sp.GetRequiredService<IScopedService>()))

The lamda function receives the root container when resolving types in the scoped container.

Attaching test case. Not XUnit :(

I can rewrite the test in XUnit and perhaps reuse some of the existing test structures if this is an issue that you feel should be addressed.

// Andreas
UnitTestScopes.txt

@pakrym
Copy link
Contributor

pakrym commented Nov 23, 2018

I'll add a spec test. You might want to file a similar issue in https://github.com/unitycontainer/microsoft-dependency-injection/issues.

cc @ENikS

@ENikS
Copy link
Contributor

ENikS commented Dec 6, 2018

@aarrgard @pakrym I will take a look

@ENikS
Copy link
Contributor

ENikS commented Dec 10, 2018

I fixed it in Unity.DI. xUnit tests are available here

@aarrgard
Copy link
Author

Looks good. Just out of curiosity. Is this test intended for just th unity container is is is something that should be tested i the generic test suite where the container is created using an abstract method. Since the https://github.com/unitycontainer/microsoft-dependency-injection passes all the tests i thougth it would be a good idea to add one that addresses the problem for all the IoC frameworks that want to be compatible. Perhaps I´m missing something. Just my 2 cents.

@pakrym
Copy link
Contributor

pakrym commented Dec 17, 2018

We should add a spec test too. This scenario was implied but not tested so it should pass on all supported containers.

@Anipik Anipik transferred this issue from dotnet/extensions Mar 22, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added area-Extensions-DependencyInjection untriaged New issue has not been triaged by the area owner labels Mar 22, 2021
@ghost
Copy link

ghost commented Mar 22, 2021

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

Issue Details

We are currently using Unity in some of our projects and want to use it with asp.net core. To enable this feature we use the "Unity.Microsoft.DependencyInjection - 2.1.1". We also register some types in the default ServiceCollection provided here that then resolves types from the Unity registrations. There seems to be an issue how unity resolves object by using lambda functions. Since all the DI tests runs successfully using the Unity bridge perhaps the "Microsoft.Extensions.DependencyInjection.Specification.Tests" needs to address this.

The issue is related to scoped containers.

  1. Create and register a transient type that takes a scoped type as parameter.
  2. Create service provider
  3. Create scoped provider
  4. Make sure that the scoped type comes from the child container when getting the transient type.

This works in Unity if you register the type with
serviceCollection.AddTransient<ITransientService, TransientService>()
but not if you register it with
serviceCollection.AddTransient(sp=>new TransientService(sp.GetRequiredService()))

The lamda function receives the root container when resolving types in the scoped container.

Attaching test case. Not XUnit :(

I can rewrite the test in XUnit and perhaps reuse some of the existing test structures if this is an issue that you feel should be addressed.

// Andreas
UnitTestScopes.txt

Author: aarrgard
Assignees: -
Labels:

area-Extensions-DependencyInjection, untriaged

Milestone: -

@ENikS
Copy link
Contributor

ENikS commented Mar 31, 2021

serviceCollection.AddTransient(sp=>new TransientService(sp.GetRequiredService()))

You are resolving service from root inside your lambda, how sp should know about the scope?

@aarrgard
Copy link
Author

When I tested this 2,5 years ago this registration works:

serviceCollection.AddTransient<ITransientService, TransientService>()

How does the service provider know about the scope in that case when resolving instances?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants