繁体   English   中英

如何在 AWS Lambda 项目中存储和加载自定义 json 应用程序设置和连接字符串?

[英]How to store and load custom json appsettings and Connection Strings in an AWS Lambda Project?

我正在 Visual Studio 中开发 Alexa Skills Kit 应用程序,并在 .NET Core 2.0 中创建了 AWS Lambda 项目。 创建项目时,它会自动附带一个名为 aws-lambda-tools-defaults.json 的文件。 此处的设置由 AWS Lambda 函数使用。

现在,我想存储我自己的应用程序设置和连接字符串以供应用程序使用。 关于如何存储我的设置并在代码中获取它们的首选路线是什么?

我已经添加了名为 Microsoft.Extensions.Configuration.Json 的 NuGet 包。 但我不确定我从这里做什么。

1) 我需要创建另一个 json 文件,还是可以使用现有文件 aws-lambda-tools-defaults.json? 如果我确实需要创建一个新文件,它应该叫什么?

2) 是否需要将构造函数添加到 Function.cs 文件中才能加载 json 文件?

3)我可以编写哪些代码来实际获取我的设置和连接字符串?

我在这里有点迷茫,所以真的任何建议都会有所帮助! 谢谢!

  1. 是的,您确实需要创建另一个文件,这些文件将根据环境使用。 但是, aws-lambda-tools-defaults.json不是需要保存连接字符串和其他应用程序配置设置的文件。 相反,它是appsettings.json文件。

在此处输入图片说明

2 和 3. 从这里开始,您可以按照微软关于如何使用配置文件的说明进行操作

基本上,在Startup文件中,您可以通过IConfiguration获取配置:

Configuration.GetValue<string>("myConfigValue");

具体来说,对于使用 netcore 2.1 的 aws serverless 应用程序,如果您想使用appsettings.{environment}.json ,您可以执行以下操作:

    /// <summary>
    /// The Main function can be used to run the ASP.NET Core application locally using the Kestrel webserver.
    /// </summary>
    public static class LocalEntryPoint
    {
        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((builderContext, config) =>
                {
                    string defaultAppSettingsFile = "appsettings.json";

                    // because of a bug in Visual Studio 2017 version 15.8.2 (newest at this time) with NetCore projects, simply changing the configuration won't work
                    // you have to Close, and then Re-open the project in order for the Conditional Compilation Symbols to be taken in effect
#if Debug
                    string appSettingsFileToAdd = "appsettings.Debug.json";
#elif Beta
                    string appSettingsFileToAdd = "appsettings.Beta.json";
#elif Release
                    string appSettingsFileToAdd = "appsettings.Release.json";
#else
                    string appSettingsFileToAdd = "appsettings.Debug.json";
#endif
                    config
                        .AddJsonFile(
                            defaultAppSettingsFile,
                            optional: false,
                            reloadOnChange: true)
                        .AddJsonFile(
                            appSettingsFileToAdd,
                            optional: true,
                            reloadOnChange: true);
                })
                .UseStartup<Startup>()
                .Build();

        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }
    }

您需要为它创建自己的 ServiceCollection 和 IConfiguration。 我创建了一些名为 ILambdaHost 的吸引力,它以类似于 IHose 的方式实现。 我不建议使用 IWebHost,因为您不需要 Lambda 中的 Web 主机功能,这会带来很多开销。

以下是创建配置类并将 appsettings.json 添加到其中的方法。

using Microsoft.Extensions.Configuration;
using System;
using System.IO;

namespace MyNamespace
{
    public static class LambdaConfigurationBuilder
    {
         public static IConfiguration Build() => new ConfigurationBuilder()
           .SetBasePath(Directory.GetCurrentDirectory())
           .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
           .Build();
    }
}



   

暂无
暂无

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

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