Closed sramananthula closed 1 year ago
@sramananthula Hi,
So, are the options not injected with the current DryIoc adapter?
I have tested it in the example project for #520 (https://github.com/dadhi/DryIoc/blob/9ea3bd9a2a58034792a0df30ecfe86af2ffdd574/samples/GHIssue520/Program.cs#L11), and DryIoc has injected the options just fine https://github.com/dadhi/DryIoc/blob/9ea3bd9a2a58034792a0df30ecfe86af2ffdd574/samples/GHIssue520/Program.cs#L169
Hi, I tested with my project and it is giving following exception.
DryIoc.MefAttributedModel.AttributedModelException
HResult=0x80131509
Message=code: Error._containerErrorCount;
message: Unable to find single constructor nor marked with System.ComponentModel.Composition.ImportingConstructorAttribute nor default constructor in Microsoft.Extensions.Options.OptionsFactory
Here is my code: public class MyConfigurationProvider : IConfigurationProvider {
//Constructor
public MyConfigurationProvider([Import]IOptions<Settings> options)
{
_options = options.Value;
}
}
///Option Class public class Settings { public string URL{ get; set; }
public string KeyOne { get; set; }
}
//appsettings.json { "Settings": { "ChartIQ": "http://iichart.fmr.com/branch/develop/pure/examples/chart.html", "KeyOne": "I am from config settings" } }
//configure options
builder.Services.Configure
//Failing here if the constructor takes IOptions
I see that you are using MEF extension?
And here is the thing that you do not control (you cannot put the Export on top of it) and should Register
it: Microsoft.Extensions.Options.OptionsFactory<Fmr.SuperNova.Settings>
.
Try to register OptionFactory<>
in container and specify what constructor to use via made: Made.Of(optionFactoryConstructor)
parameter.
for each option do we need to register this way then along with get section from config ?
Instance.Container.Register<Microsoft.Extensions.Options.IOptionsFactory
Instance.Container.Register
DryIoc.ContainerException
HResult=0x80131509
Message=code: Error.RegisteringAbstractImplementationTypeAndNoFactoryMethod;
message: Registering abstract implementation type Microsoft.Extensions.Options.IOptionsFactory
@sramananthula You are registering the interface instead of the implementation, that's why you are getting the error.
Looking at the MS docs https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-6.0#use-di-services-to-configure-options
you may just call AddOptions
to add them to services.
Btw, I don't see anywhere in this issue that you're doing this.
I registered Container.Register<Microsoft.Extensions.Options.OptionsFactory
public OptionService(IOptions<Settings> options)
{
_options = options.Value;
}
}
@sramananthula What is the error?
Sorry. i did not added madeof the container register method. I just added like this. I am not sure whether it is correct or not.
Container.Register<OptionsFactory
here is the error:
DryIoc.ContainerException
HResult=0x80131509
Message=code: Error.UnableToResolveUnknownService;
message: Unable to resolve Microsoft.Extensions.Options.IOptions
@sramananthula Ok, You are using the Made.Of
wrong. Read the docs.
Regarding the initial problem with options... I don't know what is wrong with your case, but I have the working example here.
You may
dotnet run
or debug and see the output that options are injected just fine.You may try to run it in isolation as well by replacing the line test.csproj
<ProjectReference Include="..\..\src\DryIoc.Microsoft.DependencyInjection\DryIoc.Microsoft.DependencyInjection.csproj" />
with
<PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="6.1.0" />
Hope it will be fixed for you @sramananthula by #544
In ASP.Net, IOption pattern used for read config values from appsettings.json and populate concrete class. That is injected where ever required. So it would be great if we have such with DryIoc pattern.
Expectation is if could achieve with Dry Ioc pattern it would be great.
services.Configure(
configurationRoot.GetSection(
key: nameof(TransientFaultHandlingOptions)));
Here is the IOption pattern applied in ASP.Net, (https://docs.microsoft.com/en-us/dotnet/core/extensions/options)
For example, to read the configuration values from an appsettings.json file: {
"TransientFaultHandlingOptions": { "Enabled": true, "AutoRetryDelay": "00:00:07" } }
Create the following TransientFaultHandlingOptions class for the above setting.
public class TransientFaultHandlingOptions { public bool Enabled { get; set; } public TimeSpan AutoRetryDelay { get; set; } }
In the program.cs, (application start up) read the configuration.
IConfigurationRoot configurationRoot = configuration.Build();
And the consumer uses these settings like below:
public class ExampleService { private readonly TransientFaultHandlingOptions _options;
}