10

I would like to pass a value to an IOptions<T> parameter.

I can only find examples of using IOptions<T> with configuration, but I need to define a custom value when invoking a method.

5
  • Implement the interface into a concrete class and pass this as a parameter...? Alternatively if code allows and options isn't required you may be able to pass a null. Commented Dec 28, 2018 at 18:18
  • @Adriani6 it fails if I pass null. it's in a constructor and I'm invoking base so I can't really create new objects. I'll update the question with more code Commented Dec 28, 2018 at 18:25
  • 1
    Does this answer help? Commented Dec 28, 2018 at 18:30
  • Possible duplicate of IOptions Injection Commented Dec 28, 2018 at 18:38
  • 1
    @Hasan please read the question. I can only find examples of IOptions with configuration and injection. that's NOT what I was looking for Commented Dec 28, 2018 at 18:42

2 Answers 2

16

I am assuming that you are asking how to create a custom IOptions<TOptionClass> value where you specify the instance of T to be used. Here is how you can do that:

Let's assume you have a class called IdentityOptions as in your example.

First create an instance of it:

var optionsInstance = new IdentityOptions();
// ... set properties on it as needed

Then convert it into an Option-container:

IOptions<IdentityOptions> optionParameter = Options.Create(optionsInstance);

See MSDN.

Update: I was a few seconds slower than the OP's own solution. Let me add a possible extension-method solution so this answer still has some added value (not tested):

public static IOptions<TOptions> AsIOption<TOptions>(this TOptions optionInstance) where TOptions : class, new()
{
    return Microsoft.Extensions.Options.Options.Create(optionInstance);
}

Which then you can use as optionInstance.AsIOption(). I'm not sure if it's worth the effort (I don't like to pollute the Object class if not necessary), but certainly possible, and might get useful if you use this technique at many different places.

Sign up to request clarification or add additional context in comments.

Comments

0

My solution:

public LCSignInManager(UserManager<Profile> userManager, IdentityDbContext db, IHttpContextAccessor contextAccessor, IUserClaimsPrincipalFactory<Profile> claimsFactory, IOptions<IdentityOptions> optionsAccessor = null) : base(userManager, contextAccessor, claimsFactory, optionsAccessor, new LoggerFactory().CreateLogger<LCSignInManager>(), (IAuthenticationSchemeProvider)new AuthenticationSchemeProvider(GetOption()).GetDefaultAuthenticateSchemeAsync().Result)
{
    _userManager = userManager;
    this.DbContext = db;
}

private static IOptions<AuthenticationOptions> GetOption()
{
    var settings = new AuthenticationOptions
        {

        };

    IOptions<AuthenticationOptions> result = Microsoft.Extensions.Options.Options.Create(settings);

    return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.