简体   繁体   English

从 appsettings.json 将多个端点路由添加到 .net 核心控制台应用程序

[英]Adding multiple endpoint routes to .net core console app from appsettings.json

I'm using .net core 3.1 to build a console app that acts as an event handler API.我正在使用 .net 核心 3.1 来构建一个控制台应用程序,该应用程序充当事件处理程序 API。

The app captures changes to a database and directs those changes to other APIs, in real-time.该应用程序实时捕获对数据库的更改并将这些更改定向到其他 API。 Updates to "customer" go to "customerAPI", "product" goes to "productAPI" and so on.将“customer”go 更新为“customerAPI”,“product”更新为“productAPI”等等。 This means that I have an appsettings.Local.json that looks like this:这意味着我有一个 appsettings.Local.json 看起来像这样:

 "DBConnectionStrings": {
    "DefaultConnection": "AccountEndpoint=(my account)",
    "SourceDatabaseName": "MyDB",
    "SourceContainerName": "MySource",
    "LeasesContainerName": "MyLease",
    "PartitionKey": "/id"
  },
  "EndpointAPIStrings": {
    "Endpoint1": {
      "EndpointUrl": "https://localhost:7777",
      "Username": "myusername1",
      "Password": "mypassword1",
    "Endpoint2": {
      "EndpointUrl": "https://localhost:8888",
      "Username": "myusername2",
      "Password": "mypassword2",
    "Endpoint3": {
      "EndpointUrl": "https://localhost:9999",
      "Username": "myusername3",
      "Password": "mypassword3"
    ...
    }

I am currently using a crappy method of declaring them as EnvironmentVariables to get them from my Main where the configuration is built to my CallAPI Task.我目前正在使用一种糟糕的方法将它们声明为 EnvironmentVariables 以从我的 Main 中获取它们,在该 Main 中将配置构建到我的 CallAPI 任务中。

Main:主要的:

public static async Task Main(string[] args)
{
    ...
    IConfiguration configuration = BuildConfiguration(environmentName);
    CosmosClient cosmosClient = BuildCosmosClient(configuration);

    Environment.SetEnvironmentVariable("EndpointUrl", configuration["EndpointAPIStrings:Endpoint1:EndpointURL"]);
    Environment.SetEnvironmentVariable("Username", configuration["EndpointAPIStrings:Endpoint1:Username"]);
    Environment.SetEnvironmentVariable("Password", configuration["EndpointAPIStrings:Endpoint1:Password"]);
    ...
}

Delegate function:代表 function:

...
if (entityType == "myproduct")
{
    var entity = "products";
    var result = await Task.Run(() => CallAPIAsync(entity, item));
}
...

Task CallAPI:任务调用API:

public static async Task<HttpResponseMessage> CallAPIAsync(string entity, ProcessedItem item)
{
    using (var client = new HttpClient())
    {
        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
        var endpointUrl = Environment.GetEnvironmentVariable("EndpointUrl");
        var uri = new Uri($"{endpointUrl}/api/{entity}/{item.Id}/propagate");
        string username = Environment.GetEnvironmentVariable("Username");
        string password = Environment.GetEnvironmentVariable("Password");
        ...
    }
}

This obviously only works for the first endpoint and ignores the others.这显然只适用于第一个端点,而忽略其他端点。

How can I refactor this to get the values into my CallAPI Task for all EndpointAPIStrings?如何重构它以将所有 EndpointAPIStrings 的值放入我的 CallAPI 任务中?

You can create a class for it and read the values into that class.您可以为其创建一个 class 并将值读入该 class。 Also changing it to a list in the JSON would be good.也将其更改为 JSON 中的列表会很好。 Steps I would do:我会做的步骤:

  1. Change 'EndpointAPIStrings' into an array:将“EndpointAPIStrings”更改为数组:

     { "EndpointAPIStrings":[ { "Id":"Endpoint1", "EndpointUrl":"https://localhost:7777", "Username":"myusername1", "Password":"mypassword1" }, { "Id":"Endpoint2", "EndpointUrl":"https://localhost:8888", "Username":"myusername2", "Password":"mypassword2" }, { "Id":"Endpoint3", "EndpointUrl":"https://localhost:9999", "Username":"myusername3", "Password":"mypassword3" } ] }
  2. Create a C# class defining the objects in the JSON array:创建一个 C# class 定义 JSON 数组中的对象:

     public sealed class EndPoint { public string Id { get; set; } public string EndPointUrl { get; set; } public string Username { get; set; } public string Password { get; set; } }
  3. Change data retrieval from the configuration:从配置中检索更改数据:

     IConfiguration configuration = BuildConfiguration(environmentName); CosmosClient cosmosClient = BuildCosmosClient(configuration); List<EndPoint> endPoints = configuration.GetSection("EndPointAPIStrings").Get<List<EndPoint>>();

Now you have all your endpoints in the endPoints variable.现在您的所有端点都在endPoints变量中。 You can remove and add properties into the JSON how you like and the only thing you need to do is change the class accordingly.您可以按照自己的喜好删除和添加属性到 JSON 中,您唯一需要做的就是相应地更改 class。 Please note that you need the same names in the JSON and in the C# class in order to get a successful mapping.请注意,您需要在 JSON 和 C# class 中使用相同的名称才能成功映射。

I've done this in a Windows Service .net Core 3.1 app, pretty similar.我在 Windows 服务 .net Core 3.1 应用程序中完成了此操作,非常相似。 Essentially when you call your IHostBuilder function in program.cs本质上,当您在 program.cs 中调用 IHostBuilder function

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseWindowsService()
                .ConfigureLogging(loggerFactory => loggerFactory.AddEventLog())
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<Worker>();
                });

You get access to your configuration variables from appsettings.json by default.默认情况下,您可以从 appsettings.json 访问配置变量。 Which can then be accessed in your main startup or execute function:然后可以在您的主启动中访问或执行 function:

private readonly ILogger<Worker> _logger;

private readonly IServiceScopeFactory _serviceScopeFactory;

private readonly IConfiguration _config;

public Worker(ILogger<Worker> logger, IServiceScopeFactory serviceScopeFactory, IConfiguration config)
            {
                _logger = logger;
                _serviceScopeFactory = serviceScopeFactory;
                _config = config;
            }  

And then in your main or execute function:然后在你的主要或执行function:

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            // Must be a scoped process in order to run correctly
            using var scope = _serviceScopeFactory.CreateScope();
            // Start timer and begin log
            var startTime = DateTime.UtcNow;
            var env = _config.GetValue<string>("ENV");
            var stageTable = _config.GetValue<string>("StageTable");
            var prevTable = _config.GetValue<string>("PrevTable");
            var mainTable = _config.GetValue<string>("MainTable");
            var sqlConnectionString = _config.GetValue<string>("SqlConnString_" + env);
            var excelConnectionString = _config.GetValue<string>("ExcelConnectionString1") +
                                        _config.GetValue<string>("ExcelFilePath_" + env) +
                                        _config.GetValue<string>("ExcelFileName") +
                                        _config.GetValue<string>("ExcelConnectionString2");

With an appsettings.json like:使用 appsettings.json 像:

"ENV": "LOCAL",
"StageTable": "Staging",
"PrevTable": "Previous",
"MainTable": "Courses",

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 appsettings.json 中的哨兵配置与 .Net Core 3 控制台应用程序中的 Serilog - Sentry configuration in appsettings.json with Serilog in .Net Core 3 Console App Docker 中 .NET Core 应用程序的 appSettings.json? - appSettings.json for .NET Core app in Docker? 如何在控制台应用程序 (.net Core) 中将数据写入 appsettings.json? - How to write data to appsettings.json in a console application (.net Core)? .NET Core 3.1 从 appsettings.json 为控制台应用程序加载配置 - .NET Core 3.1 loading config from appsettings.json for console application 如何使用 .Net 核心控制台应用程序从 appsettings.json 读取部分? - How to read a section from appsettings.json using .Net core console application? 如何在使用 .NET Core 的控制台应用程序中从 appsettings.json 获取值? - How to get values from appsettings.json in a console application using .NET Core? 如何在 asp.net 核心控制台应用程序中的 class 程序中的 appsettings.json 中获取价值 - How to get value from appsettings.json in Program class in asp.net core console application 在 .NET 6 控制台应用程序中读取 appsettings.json 文件 - Reading appsettings.json file in .NET 6 console app ASP.NET Core 应用程序不会从输出目录中读取 appsettings.json,而是从项目一中读取 - ASP.NET Core app does not read appsettings.json from output directory, but from the project one 在控制台应用程序中从另一个 class 调用 appsettings.json 数据 - Calling appsettings.json data from another class in a console app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM