4est
4est

Reputation: 3168

Inject an instance of an IOptions<>

I need to access the value of my JwtSettings. I try to inject an instance of an IOptions<> class into the constructor of my class, but I got the problem:

 public class JwtHandler : IJwtHandler
 {
    private readonly JwtSettings _jwtSettings;

    public JwtHandler(IOptions<JwtSettings> jwtSettings)
    {
        jwtSettings = _jwtSettings.Value;
    } 
 }

I have error:

The type or namespace name 'IOptions<>' could not be found 
(are you missing a using directive or an assembly reference?)

When Im adding the using:

using Microsoft.Extensions.Options;

VS is telling me that it is unnecesary.

Updated: My Assemblies:

Microsoft.IdentityModel.Tokens    
System.IdentityModel.Tokens.Jwt

Upvotes: 2

Views: 4070

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239290

IOptions<T> is definitely in the Microsoft.Extensions.Options namespace, so that is the using you need:

using Microsoft.Extensions.Options

As far as why it might be telling you it's unnecessary, that's a brain-scratcher. The only thing I can think of is that something is borked in your project. Open your project folder and delete the bin and obj directories. Then rebuild your project. If it's still not working, you may have a version conflict. Check any referenced projects, and if any are explicitly including Microsoft.Extensions.Configuration, Microsoft.AspNetCore, etc. ensure that they're all referencing the same versions of the those NuGet packages.

Upvotes: 5

Related Questions