在我的.NET Core 8 API中,接口如下:

//接口

public interface ITokenService { 
   string Generate(UserDetail user);
}

public interface IAccessTokenService : ITokenService { }

public interface IRefreshTokenService : ITokenService { }
      
}
    

刷新令牌服务类:-

public class RefreshTokenService : IRefreshTokenService { 

       private readonly ITokenGenerator _tokenGenerator;
 private readonly JWTSettings _jwtSettings;
 public RefreshTokenService(IOptions<JWTSettings> settings, ITokenGenerator tokenGenerator)
 => (_tokenGenerator, _jwtSettings) = (tokenGenerator, settings.Value);

 public string Generate(UserDetail user) => _tokenGenerator.Generate(_jwtSettings.RefreshTokenSecret, _jwtSettings.Issuer, _jwtSettings.Audience, _jwtSettings.RefreshTokenExpiry);
}

AccessTokenService类:-

   public class AccessTokenService : IAccessTokenService
   {
      private readonly ITokenGenerator _tokenGenerator;
      private readonly JWTSettings _jwtSettings;
      public AccessTokenService(ITokenGenerator tokenGenerator, JWTSettings jwtSettings) =>
             (_tokenGenerator, _jwtSettings) = (tokenGenerator, jwtSettings);
    
    public string Generate(UserDetail user)
    {
        List<Claim> claims = new List<Claim>() {};

        return _tokenGenerator.Generate(_jwtSettings.AccessTokenSecret, _jwtSettings.Issuer, _jwtSettings.Audience, _jwtSettings.RefreshTokenExpiry,claims);
    }
}

我已经注册了Statup.cs个班级

services.AddScoped<ITokenService, RefreshTokenService>();
services.AddScoped<ITokenService, AccessTokenService>();

services.AddTransient<IAccessTokenService, AccessTokenService>();
services.AddTransient<IRefreshTokenService, RefreshTokenService>(); 
--------------Other services here---------------

我正在使用访问令牌和刷新令牌类的服务类

public class SomeClass : ISomeService{
    private readonly IRefreshTokenService _refreshTokenService;
    private readonly IAccessTokenService _accessTokenService;
    
     public SomeClass(IRefreshTokenService refreshTokenService, 
      IAccessTokenService accessTokenService){
           _refreshTokenService =  refreshTokenService;
           _accessTokenService = accessTokenService;
      }
}

但得到了错误:

无法构造某些服务(验证服务描述符的ServiceType时出错: API.Services.IAccessTokenService Lifetime:Temporent ImplementationType:Services.AccessTokenService‘:try 激活’Services.AccessTokenService`时,无法解析类型为‘Models.JWTSettings’的服务.如何在此处解析依赖关系.

推荐答案

似乎使用options pattern进行配置,则需要为IOptions...<TOptions>接口解析一个接口,通常是IOptions<TOptions>接口本身,而不是直接解析配置类.例如:

public AccessTokenService(ITokenGenerator tokenGenerator, IOptions<JWTSettings> jwtSettingsOpts) =>
     (_tokenGenerator, _jwtSettings) = (tokenGenerator, jwtSettingsOpts.Value);

请注意,在RefreshTokenService年中,您所做的就是这样:

public RefreshTokenService(IOptions<JWTSettings> settings, ITokenGenerator tokenGenerator) 
=> (_tokenGenerator, _jwtSettings) = (tokenGenerator, settings.Value);

Csharp相关问答推荐

ASP.NET Core 8 Cors标题未显示

在Linq中调用需要limit和offset的方法''''

实现List T,为什么LINQ之后它不会返回MyList?<>(无法强制转换WhereListIterator `1类型的对象)'

ASP.NET Core 8.0 JWT验证问题:尽管令牌有效,但SecurityTokenNoExpirationError异常

MAUI查询参数单一字符串项将不起作用

C#.NET依赖项注入顺序澄清

Nuget包Serilog.Sinks.AwsCloudwatch引发TypeLoadExceptions,因为父类型是密封的

TeamsBot SendActivityActivityTypes与ActivityTypes同步.键入不再起作用

如何在Cosmos SDK中控制超时、重试和重试之间的延迟?

为什么方法的值在SELECT方法中不会更改?

如何让两个.NET版本不兼容的项目对话?

链接到字典字符串.拆分为(.Key,.Value)

如何在同一成员上组合[JsonPropertyName]和[ObservableProperty]?

如何让游戏对象在切换场景时被销毁,但在开始新游戏时重新锁定

JSON串行化程序问题.SQLite中的空值

Maui:更改代码中的绑定字符串不会更新UI,除非重新生成字符串(MVVM)

删除MudRadio时,MudRadioGroup未 Select 正确的MudRadio

如何保存具有多个重叠图片框的图片框?

.NET6最小API:操作.MapGet之后的响应

无法通过服务控制台启动.NET Core 6.0服务(错误1053)