繁体   English   中英

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

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

我正在使用 .net 核心 3.1 来构建一个控制台应用程序,该应用程序充当事件处理程序 API。

该应用程序实时捕获对数据库的更改并将这些更改定向到其他 API。 将“customer”go 更新为“customerAPI”,“product”更新为“productAPI”等等。 这意味着我有一个 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"
    ...
    }

我目前正在使用一种糟糕的方法将它们声明为 EnvironmentVariables 以从我的 Main 中获取它们,在该 Main 中将配置构建到我的 CallAPI 任务中。

主要的:

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"]);
    ...
}

代表 function:

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

任务调用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");
        ...
    }
}

这显然只适用于第一个端点,而忽略其他端点。

如何重构它以将所有 EndpointAPIStrings 的值放入我的 CallAPI 任务中?

您可以为其创建一个 class 并将值读入该 class。 也将其更改为 JSON 中的列表会很好。 我会做的步骤:

  1. 将“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. 创建一个 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. 从配置中检索更改数据:

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

现在您的所有端点都在endPoints变量中。 您可以按照自己的喜好删除和添加属性到 JSON 中,您唯一需要做的就是相应地更改 class。 请注意,您需要在 JSON 和 C# class 中使用相同的名称才能成功映射。

我在 Windows 服务 .net Core 3.1 应用程序中完成了此操作,非常相似。 本质上,当您在 program.cs 中调用 IHostBuilder function

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

默认情况下,您可以从 appsettings.json 访问配置变量。 然后可以在您的主启动中访问或执行 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;
            }  

然后在你的主要或执行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");

使用 appsettings.json 像:

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM