【问题标题】:AspNetCore 3.1 Web API serverless - how to manage lazy connection initializationAspNetCore 3.1 Web API serverless - 如何管理延迟连接初始化
【发布时间】:2020-09-11 23:44:21
【问题描述】:

我正在构建 AspNetCore 3.1 Web API 无服务器 lambda 应用程序。

我被告知连接(例如:Redis 连接)初始化应在函数处理程序之外完成。这是为了更好地利用连接池。

Azure 团队也提出了相同的建议: https://docs.microsoft.com/en-us/azure/azure-cache-for-redis/cache-dotnet-core-quickstart

我期待看到我们如何在使用 Amazon.Lambda.AspNetCoreServer 时实现它

https://github.com/aws/aws-lambda-dotnet/tree/master/Libraries/src/Amazon.Lambda.AspNetCoreServer#bootstrapping-application-iwebhostbuilder-vs-ihostbuilder

高度赞赏响应或任何其他解决问题的指针。

【问题讨论】:

    标签: amazon-web-services asp.net-core .net-core aws-lambda


    【解决方案1】:

    作为一个例子。

    namespace blankCsharp
    {
      public class Function
      {
        private static RedisClient redisClient; <<
    
        static Function() {
          initialize();
        }
    
        static async void initialize() {
          AWSSDKHandler.RegisterXRayForAllServices();
          redisClient = new RedisClient('1.2.3.4', 6379, 'username', 'password'); << initialised and reusable between invocations.
          await callLambda();
        }
    
        public async Task<AccountUsage> FunctionHandler(SQSEvent invocationEvent, ILambdaContext context)
        {
          return redisClient.get('dhddhdhdud')
        }
      }
    }
    

    我相信这就是您想要实现的目标。通过在初始化部分设置它,只要不是冷启动,它就应该能够保持调用之间的连接。

    您应该记住,默认情况下 Lambda 会遍历 Internet,因此您的 Redis K/V 存储需要公开托管。

    如果您想私下运行此程序,您需要考虑通过attaching it to a VPC 将您的 Lambda 迁移到私有子网中。

    【讨论】:

    • 谢谢你的好例子。如果您有一个如何使用 APIGatewayProxyFunction 使其在 AspNetCore Web API 中工作的示例,我将不胜感激
    猜你喜欢
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 2012-02-24
    • 2020-10-08
    • 2017-11-11
    • 1970-01-01
    相关资源
    最近更新 更多