从1.0.0-rc1-final中的appsettings.json读取值 [英] Read a value from appsettings.json in 1.0.0-rc1-final

查看:75
本文介绍了从1.0.0-rc1-final中的appsettings.json读取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的具体班级之一中。我有方法。

In one of my concrete class. I have the method.

public class Call : ICall
{
    ......
    public  Task<HttpResponseMessage> GetHttpResponseMessageFromDeviceAndDataService()
    {
        var client = new HttpClient();
        var uri = new Uri("http://localhost:30151");
        var response =  GetAsyncHttpResponseMessage(client, uri);
        return response;
    }

现在我将网址放入appsettings.json中。

Now I put the url into appsettings.json.

{
    "AppSettings": {
       "uri": "http://localhost:30151"
    }
 }

我创建了一个 Startup.cs

 public class Startup
{
    public IConfiguration Configuration { get; set; }
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json");

        Configuration = builder.Build();
    }
}

现在我被卡住了。

编辑

顺便说一句,我没有控制器,

By the way, I don't have a controller, it is a console application.

推荐答案

从appSettings.json读取配置的首选方法是使用依赖项注入和内置或(或第3方)IoC容器。您需要做的就是将配置部分传递给 Configure 方法。

The preferred way to read configuration from appSettings.json is using dependency injection and the built or (or 3rd party) IoC container. All you need is to pass the configuration section to the Configure method.

public class AppSettings
{
    public int NoRooms { get; set; }
    public string Uri { get; set; }
}

services.Configure<AppSettings>(Configuration.GetSection("appsettings"));

这样,您不必手动设置值或初始化 AppSettings 类。

This way you don't have to manually set the values or initialize the AppSettings class.

并在您的服务中使用它:

And use it in your service:

public class Call : ICall
{
    private readonly AppSettings appSettings;

    public Call(IOptions<AppSettings> appSettings) 
    {
        this.appSettings = appSetings.Value;
    }

    public Task<HttpResponseMessage>GetHttpResponseMessageFromDeviceAndDataService()
    {
        var client = new HttpClient();
        var uri = new Uri(appSettings.Uri);
        var response =  GetAsyncHttpResponseMessage(client, uri);
        return response;
    }
}

IoC容器也可以在控制台应用程序中使用,您只需要自己引导即可。 ServiceCollection 类没有依赖关系,可以正常实例化,完成配置后,将其转换为 IServiceProvider 并用它解决您的主类,它将解决所有其他依赖项。

The IoC Container can also be used in a console application, you just got to bootstrap it yourself. The ServiceCollection class has no dependencies and can be instantiated normally and when you are done configuring, convert it to an IServiceProvider and resolve your main class with it and it would resolve all other dependencies.

public class Program
{
    public static void Main(string[] args) 
    {
        var configurationBuilder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json");
        var configuration = configurationBuilder.Build()
            .ReloadOnChanged("appsettings.json");

        var services = new ServiceCollection();
        services.Configure<AppSettings>(configuration.GetSection("appsettings"));
        services.AddTransient<ICall, Call>();
        // add other services

        // after configuring, build the IoC container
        IServiceProvider provider = services.BuildServiceProvider();

        Program program = provider.GetService<Program>();

        // run the application, in a console application we got to wait synchronously
        program.Wait();
    }

    private readonly ICall callService;
    // your programs main entry point
    public Program(ICall callService) 
    {
        this.callService = callService;
    }

    public async Task Run()
    {
         HttpResponseMessage result = await call.GetHttpResponseMessageFromDeviceAndDataService();

         // do something with the result
    } 
}

这篇关于从1.0.0-rc1-final中的appsettings.json读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆