小编Xal*_*yth的帖子

在同一ConfigureServices方法中访问asp.net core中的startup.cs文件中的ConfigureServices中创建的单例

所以我一直在尝试以 JWT 分发的形式设置一个带有令牌身份验证的小型测试 api,令牌分发部分按预期工作。

然而,由于我想让 JWT 服务的方法更加通用,以允许不同类型的令牌签名(因为我更喜欢私钥/公钥对),所以我尝试在 appsettings 文件中设置更多选项,然后决定了令牌的生成方式,我开始使用依赖注入加载这些设置,直到现在我才刚刚触及这些设置。

所以当我想要将那些我设置为单例的配置类(到目前为止我读过的大多数指南都这样做了,所以我认为它有点正确)并在ConfigureServices方法中使用它们时,问题就出现了他们添加了这些参数,以便我可以使用在我看来应该设置的参数,因为我通过获取 appsettings 文件的一部分配置了上面的几行。

然而,一旦我尝试访问它们,我就没有得到任何返回,而是留下了空值。

启动.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        //the token config takes values from the appsettings.json file
        var tokenConf = Configuration.GetSection("TokenConfiguration");
        services.Configure<TokenConfiguration>(tokenConf);

        //the signing credentials are assigned in the JwtTokenService constructor
        var signingConf = …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection asp.net-web-api asp.net-core

5
推荐指数
1
解决办法
6031
查看次数