“IServicesCollect Configure is not working, results in ‘Unable to resolve service for type'”

huangapple go评论49阅读模式
英文:

IServicesCollect Configure is not working, results in "Unable to resolve service for type"

问题

I'm configuring JwtIssuerOptions using appsettings.json in an Asp.Net.Core API project (.NET 7), and here is your code:

public class JwtIssuerOptions
{
   public string Issuer { get; set; }
   public string Audience { get; set; }
   public string JwtSecretKey { get; set; }
}
public static void ConfigureJwt(this IServiceCollection services, ConfigurationManager configManager)
{
    var jwtAppSettingOptions = configManager.GetSection(nameof(JwtIssuerOptions));
    if (jwtAppSettingOptions == null)
    {
         throw new Exception("JwtIssuerOptions is null");
    }
    // jwtAppSettingOptions is not null
    services.Configure<JwtIssuerOptions>(jwtAppSettingOptions);
    // I have also tried the following:
    services.Configure<JwtIssuerOptions>(setting => jwtAppSettingOptions.Bind(setting));
    services.Configure<JwtIssuerOptions>(configManager.GetSection(nameof(JwtIssuerOptions)));
}

And you have an IdentityTokenClaimService depending on the JwtIssuerOptions:

public IdentityTokenClaimService(UserManager<ApplicationUser> userManager, JwtIssuerOptions _jwtIssuerOptions)
{
   _userManager = userManager;
   this.jwtIssuerOptions = _jwtIssuerOptions;
}

When you build the project, it gives this error:

InvalidOperationException: Unable to resolve service for type 'JwtIssuerOptions' while attempting to activate 'IdentityTokenClaimService'.

Is there anything specific you would like to know or do with this code?

英文:

I'm configuring JwtIssuerOptions using appsettings.json in a Asp.Net.Core api project (.NET 7), here is my code:

public class JwtIssuerOptions
{
   public string Issuer { get; set; }
   public string Audience { get; set; }
   public string JwtSecretKey { get; set; }
}
public static void ConfigureJwt(this IServiceCollection services, ConfigurationManager configManager)
{
    var jwtAppSettingOptions = configManager.GetSection(nameof(JwtIssuerOptions));
    if (jwtAppSettingOptions == null)
    {
         throw new Exception(&quot;JwtIssuerOptions is null&quot;);
    }
    // jwtAppSettingOptions is not null
    services.Configure&lt;JwtIssuerOptions&gt;(jwtAppSettingOptions);
    // I have also tried the following:
    services.Configure&lt;JwtIssuerOptions&gt;(setting =&gt; jwtAppSettingOptions.Bind(setting));
    services.Configure&lt;JwtIssuerOptions&gt;(configManager.GetSection(nameof(JwtIssuerOptions)));
}

And I have a IdentityTokenClaimService depending on the JwtIssuerOptions :

public IdentityTokenClaimService(UserManager&lt;ApplicationUser&gt; userManager, 
            JwtIssuerOptions _jwtIssuerOptions)
{
   _userManager = userManager;
   this.jwtIssuerOptions = _jwtIssuerOptions;
}

When I build the project, it gives this error:

InvalidOperationException: Unable to resolve service for type &#39;JwtIssuerOptions&#39; while attempting to activate &#39;IdentityTokenClaimService&#39;.

答案1

得分: 3

从错误消息来看,您直接将 JwtIssuerOptions 注入到您的服务中。

您应该注入 IOptions&lt;JwtIssuerOptions&gt; / IOptionsSnapshot&lt;JwtIssuerOptions&gt;,而不是直接注入。

例如:

public class SomeService : ISomeService
{
    private readonly IOptions&lt;JwtIssuerOptions&gt; _options;

    SomeService(IOptions&lt;JwtIssuerOptions&gt; options) 
    {
        _options = options;
    }
}
英文:

Juding From the error message, you inject JwtIssuerOptions directly into your service.

You should inject IOptions&lt;JwtIssuerOptions&gt;/IOptionsSnapshot<JwtIssuerOptions>..... instead

For Example:

public class SomeService:ISomeService
    {
        private readonly IOptions&lt;JwtIssuerOptions&gt; _options;
        SomeService(IOptions&lt;JwtIssuerOptions&gt; options) 
        {
            _options = options;
        }
    }

huangapple
  • 本文由 发表于 2023年5月10日 15:21:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定