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

IWebHostEnvironment missing in RegisterFactory container #81

Open
arian2ashk opened this issue Jul 17, 2020 · 8 comments
Open

IWebHostEnvironment missing in RegisterFactory container #81

arian2ashk opened this issue Jul 17, 2020 · 8 comments
Labels
Question ❔ Issue is a question

Comments

@arian2ashk
Copy link

arian2ashk commented Jul 17, 2020

Hi, When I use unityContainer.Resolve<IWebHostEnvironment>(); on the main container it resolve it and return a value but if I use the following code and resolve IWebHostEnvironment from child container it throws exception:

unityContainer.RegisterFactory<someType>(c =>
      {
        var iWeb = c.Resolve<IWebHostEnvironment>();
        return new someType(iWeb);
      }, new SingletonLifetimeManager());

The exception:

Unity.ResolutionFailedException: 'Resolution failed with error: No public constructor is available for type Microsoft.AspNetCore.Hosting.IWebHostEnvironment.

Why Asp.net core services are not accessible in the factory?

@ENikS
Copy link
Contributor

ENikS commented Jul 17, 2020 via email

@arian2ashk
Copy link
Author

No because it is already registered in the asp.net core built in service provider that is then registered into unity. Do you mean that I need to resolve it from the built in service provider and register it again into Unity for it to work in the factories?

@ENikS ENikS added the Question ❔ Issue is a question label Jul 17, 2020
@ENikS
Copy link
Contributor

ENikS commented Jul 17, 2020

All that is registered in core is imported into Unity. Host builder does not pass its internal registrations into user DI, some of these are implicitly created with helper functions.

You should verify if it is in the ServiceCollection that is passed into Unity. If not, just manually add it to it.

@arian2ashk
Copy link
Author

But then how is it that I can access it from my main unity container(the container which I'm registering my other services in) by doing this: unityContainer.Resolve<IWebHostEnvironment>();

@ENikS
Copy link
Contributor

ENikS commented Jul 17, 2020

Well, in that case I have no clue. I need a test case with reproduced error to help you.

@arian2ashk
Copy link
Author

I created a test project https://github.com/arian2ashk/UnityAspDotNetCoreTest were you can clone and run it and see the issue.
This line has a value and this line throws exception.

@arian2ashk
Copy link
Author

Hi @ENikS any news on this issue?

@ENikS
Copy link
Contributor

ENikS commented Jul 25, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question ❔ Issue is a question
Projects
None yet
Development

No branches or pull requests

2 participants