-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Register IHttpClientFactory inside UnityContainer #53273
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Registering the service collection is not going to be useful. The service collection is just a a list of registrations that need to be translated to the containers native format. To do that you can use the Unity.Microsoft.DependencyInjection NuGet package (written and maintained by the Unity team), which provides the needed code. While most examples show it as part of either using in asp.net core, or as part of using the Microsoft.Extensions.Hosting model, it can be used directly with the container. To use, add a using directive with the Unity.Microsoft.DependencyInjection namespace. Then on your UnityContainer there should be a BuildServiceProvider extension method that takes an IServiceCollection, where you can pass your serviceCollection to register any entries inside it. This does not return support fluent chaining, so you want to have your UnityContainer in a variable when you call this. Alternatively you could cast the return value to @ENikS This is another example where having the chainable AddServices method be public (unitycontainer/microsoft-dependency-injection#54) would probably be simpler for the user, since they are really only interested in being able to use a provided ServiceCollection extension method instead of needing to basically copy that code and manually convert to native registrations, and then need to monitor for any changes that might make in the future (this would be especially bad since some types registered are internal to Microsoft.Extensions.Http making registering them from user code tricky and fragile, but duplicating those types would be obnoxious, and an even larger maintenance burden on the user). |
@KevinCathcart Could you be so kind and show me how to do it in presented code of mine. I would appreciate. |
Tagging subscribers to this area: @dotnet/ncl Issue DetailsI am trying to register IHttpClientFactory. Firslty I've implemented it into my service:
Nevertheless i am not sure how should i correctly register it in DI UnityContainer. Can anyone help me out?
So far i've tried to add ServiceCollection and register it and i hoped i will help to resolve IHttpClientFactory but it doesn't work and i get error as folows:
When debugging error shows:
|
@Robert969696 :
IServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient();
var container = new UnityContainer()
.AddExtension(new Diagnostic())
.RegisterInstance(mapper);
container.BuildServiceProvider(serviceCollection);
container.RegisterType<IDb, Db>(new ContainerControlledLifetimeManager())
.RegisterType<IPageService, PageService>(new ContainerControlledLifetimeManager());
... You can do the I ran a simplified test version of this and this seemed to work fine for me. |
Tagging subscribers to this area: @dotnet/ncl Issue DetailsI am trying to register IHttpClientFactory. Firslty I've implemented it into my service:
Nevertheless i am not sure how should i correctly register it in DI UnityContainer. Can anyone help me out?
So far i've tried to add ServiceCollection and register it and i hoped i will help to resolve IHttpClientFactory but it doesn't work and i get error as folows:
When debugging error shows:
|
@KevinCathcart thanks, now it works ! |
Closing as answered above |
This should be fixed in v6 of Unity |
I am trying to register IHttpClientFactory. Firslty I've implemented it into my service:
Nevertheless i am not sure how should i correctly register it in DI UnityContainer. Can anyone help me out?
So far i've tried to add ServiceCollection and register it and i hoped i will help to resolve IHttpClientFactory but it doesn't work and i get error as folows:
When debugging error shows:
Unity.ResolutionFailedException: 'The current type, System.Net.Http.IHttpClientFactory, is an interface and cannot be constructed. Are you missing a type mapping?
The text was updated successfully, but these errors were encountered: