在类库项目中访问appsettings.json(在WepApi项目中)

时间:2018-11-06 12:16:31

标签: asp.net asp.net-core

我知道这个问题以前已经得到解决,但是对于我来说似乎无法解决。 Visual Studio中的解决方案包含几个项目,包括WepApi-project(asp.net核心)和一个类库项目。在类库项目中,我需要访问位于WebApi项目中的appsettings.json中的一些信息。我尝试按照不同的教程进行学习,到目前为止,已完成了以下内容:

Appsettings.json

{
   "LoginSettings": {
   "email": "test@test.com",
   "password": "12345678"
   } 
}

类建模配置(位于类库项目中):

登录设置

public class LoginSettings
{
    public string Email { get; set; }
    public string Password { get; set; }
}

Startup.cs(WebApi项目):

public void ConfigureServices(IServiceCollection services)
{

     var loginSettings = new LoginSettings
     {
         Email = Configuration["LoginSettings:email"],
         Password = Configuration["LoginSettings:password"]
     };

     services.AddSingleton(loginSettings);
     services.AddOptions();
}

我需要设置的课程

public static class Test
{
   private readonly LoginSettings settings;

   public static async Task<string> GetTokenAsync()
   {
       var email = settings.Email;

       ...
   }

}

我无法从LoginSettings获得任何信息。另外,为了将LoginSettings注入到构造函数中,我尝试使Test类变为非静态。但是,当我然后尝试实例化Test-class时,它会请求LoginSettings的实例。我真的不觉得将Test类变成非静态的,因为它的唯一目的是将字符串返回到另一个类,并且它需要LoginSettings这样做。我该怎么办?

致谢。

编辑

我使Test类变为非静态的。隐瞒时我会传递什么?

2 个答案:

答案 0 :(得分:0)

Startup.cs中:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<LoginSettings>(Configuration.GetSection("LoginSettings"));
}

在控制器中:

public class HomeController : Controller
{
    private string _email { get; set; }
    private string _password { get; set; }

    public HomeController(IOptions<LoginSettings> options)
    {
        var login = options.Value;
        _email = login.Email;
        _password = login.Password;
    }

    public IActionResult GetTokenAsync()
    {
        // _email: "test@test.com"
        // _password: "12345678"
    }
}

注意:建议您像这样在appsettings.json中命名属性名称:

{
   "LoginSettings": {
       "Email": "test@test.com",
       "Password": "12345678"
   } 
}

使用EmailPassword代替emailpassword

更新(对于I'm not going to use the LoginSettings in a controller. I'm going to use it in a class in the class library project):

public class MyLib
{
    private string _email { get; set; }
    private string _password { get; set; }

    public MyLib(IOptions<LoginSettings> options)
    {
        var login = options.Value;
        _email = login.Email;
        _password = login.Password;
    }

    public async Task<string> GetTokenAsync()
    {
        // _email: "test@test.com"
        // _password: "12345678"
    }
}

更新2

使用此库。您不能像这样使用它:

var lib = new MyLib(???)

由于MyLib(或示例中的Test)是服务,因此您在使用以下代码之前将其传递给另一个构造函数:

public class AnotherClass
{
    private MyLib _lib { get; set; }

    public AnotherClass(MyLib lib)
    {
        _lib = lib;
    }

    public async Task UseIt()
    {
        var token = await _lib.GetTokenAsync();
    }
}

答案 1 :(得分:0)

好的,我正在尝试说明它在我的情况下如何工作。

在我的 ClassLibrary 中,我具有以下扩展方法

public static class MyClassLibraryExtensions
{
    public static IServiceCollection AddMyClassLibrary(this IServiceCollection services, LoginSettings options)
    {
        services.AddSingleton<LoginSettings>(options);
        return services;
    }
}

然后在 Web.API 项目中

public void ConfigureServices(IServiceCollection services)
{
    services.AddOptions();

    var loginSettings = new LoginSettings
    {
        Email = Configuration["LoginSettings:email"],
        Password = Configuration["LoginSettings:password"]
    };

    services.AddMyClassLibrary(loginSettings);
    ...
}

并使用它

public class Test
{
   private readonly LoginSettings _settings;

   public Test(LoginSettings loginSettings)
   {
        _settings = loginSettings;
   }

   public static async Task<string> GetTokenAsync()
   {
       var email = _settings.Email;

       ...
   }
}