You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a situation where I need to be able to use a IHttpClientFactory that has been configured globally (in the root container) in my application, and additional factories that are configured on each web host that my application creates.
To this end I've been using a root unity container and then using the MDI extension to also use it for each web host I create so that they can share some common configuration.
Unfortunately, I noticed some strange behavior where only the configuration set up in the global IHttpClientFactory would be available when trying to use it in middleware, etc.
After some debugging, it looked like an issue where the various ConfigureNamedOptions<HttpClientFactoryOptions> instances added by the UseHttpClient method weren't merged together properly when added from both a parent and child container.
I wrote a small unit test in order to specifically test how the options pattern behaves when using named options on both a parent and child container to see if that was indeed the case.
Unfortunately, when running the test I encountered an unexpected exception when building the service provider in the child container: Object reference not set to an instance of an object.
Error in: RegisterType<IOptions`1,OptionsManager`1>( 'ALL', Lifetime:InjectionPerContainer)
Unit Test:
namespace UnityRepro
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using Microsoft.Extensions.Options;
using Unity;
using Unity.Microsoft.DependencyInjection;
[TestClass]
public class UnityMdiOptionsTest
{
[TestMethod]
public void TestOptions()
{
const string name = "myoptions";
IUnityContainer parent = new UnityContainer();
IServiceCollection parentServices = new ServiceCollection();
parentServices.AddOptions();
parentServices.Configure<MyOptions>(name, opt => opt.Values.Add("parent"));
parentServices.BuildServiceProvider(parent);
IOptionsMonitor<MyOptions> parentMonitor = parent.Resolve<IOptionsMonitor<MyOptions>>();
MyOptions parentOptions = parentMonitor.Get(name);
Assert.IsTrue(parentOptions.Values.Contains("parent"));
using IUnityContainer child = parent.CreateChildContainer();
IServiceCollection childServices = new ServiceCollection();
childServices.AddOptions();
childServices.Configure<MyOptions>(name, opt => opt.Values.Add("child"));
childServices.BuildServiceProvider(child); // this line throws
IOptionsMonitor<MyOptions> childMonitor = child.Resolve<IOptionsMonitor<MyOptions>>();
MyOptions childOptions = childMonitor.Get(name);
Assert.IsTrue(childOptions.Values.Contains("child"));
}
private class MyOptions
{
public List<string> Values { get; } = new List<string>();
}
}
}
The exception is thrown when building the service provider using the child container: childServices.BuildServiceProvider(child);
Am I doing something wrong here? It seems to me that this should just work, but perhaps this is an unsupported scenario.
The text was updated successfully, but these errors were encountered:
Hello,
I have a situation where I need to be able to use a
IHttpClientFactory
that has been configured globally (in the root container) in my application, and additional factories that are configured on each web host that my application creates.To this end I've been using a root unity container and then using the MDI extension to also use it for each web host I create so that they can share some common configuration.
Unfortunately, I noticed some strange behavior where only the configuration set up in the global
IHttpClientFactory
would be available when trying to use it in middleware, etc.After some debugging, it looked like an issue where the various
ConfigureNamedOptions<HttpClientFactoryOptions>
instances added by theUseHttpClient
method weren't merged together properly when added from both a parent and child container.I wrote a small unit test in order to specifically test how the options pattern behaves when using named options on both a parent and child container to see if that was indeed the case.
Unfortunately, when running the test I encountered an unexpected exception when building the service provider in the child container:
Object reference not set to an instance of an object.
Error in: RegisterType<IOptions`1,OptionsManager`1>( 'ALL', Lifetime:InjectionPerContainer)
Unit Test:
The exception is thrown when building the service provider using the child container:
childServices.BuildServiceProvider(child);
Am I doing something wrong here? It seems to me that this should just work, but perhaps this is an unsupported scenario.
The text was updated successfully, but these errors were encountered: