英文:
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("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 I have a IdentityTokenClaimService
depending on the JwtIssuerOptions
:
public IdentityTokenClaimService(UserManager<ApplicationUser> userManager,
JwtIssuerOptions _jwtIssuerOptions)
{
_userManager = userManager;
this.jwtIssuerOptions = _jwtIssuerOptions;
}
When I build the project, it gives this error:
InvalidOperationException: Unable to resolve service for type 'JwtIssuerOptions' while attempting to activate 'IdentityTokenClaimService'.
答案1
得分: 3
从错误消息来看,您直接将 JwtIssuerOptions
注入到您的服务中。
您应该注入 IOptions<JwtIssuerOptions>
/ IOptionsSnapshot<JwtIssuerOptions>
,而不是直接注入。
例如:
public class SomeService : ISomeService
{
private readonly IOptions<JwtIssuerOptions> _options;
SomeService(IOptions<JwtIssuerOptions> options)
{
_options = options;
}
}
英文:
Juding From the error message, you inject JwtIssuerOptions
directly into your service.
You should inject IOptions<JwtIssuerOptions>
/IOptionsSnapshot<JwtIssuerOptions>..... instead
For Example:
public class SomeService:ISomeService
{
private readonly IOptions<JwtIssuerOptions> _options;
SomeService(IOptions<JwtIssuerOptions> options)
{
_options = options;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论