You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET标准类库与.NET Core项目的传递依赖及AWS Lambda复用咨询

Hey there! Let's work through this AWS Lambda + reusable class library setup you're trying to build— I’ve messed around with similar configurations before, so here’s what I’ve learned to make this smooth.

Core Idea: Go with .NET Standard for Reusability

First off, your .NET Standard class library is the right call here. Unlike a console app (which is meant to be executed directly, not referenced by other projects), .NET Standard is designed specifically for cross-platform reuse. It plays nicely with AWS Lambda's .NET Core runtime, so you’ll avoid compatibility headaches down the line.

Step 1: Set Up Your Reusable Class Library Correctly
  1. Target the right framework: Make sure your class library is set to target .NET Standard 2.0—this version is widely supported by Lambda’s runtime and works seamlessly with HTML Agility Pack.
  2. Add HTML Agility Pack: Install it via NuGet (either through the Package Manager UI or run this command in your class library project directory):
    dotnet add package HtmlAgilityPack
    
  3. Write your reusable logic: Here’s a simple example of how your class might look:
    using System;
    using HtmlAgilityPack;
    
    namespace LambdaReusableLogic
    {
        public class HtmlContentProcessor
        {
            public string GetPageTitle(string rawHtml)
            {
                var htmlDoc = new HtmlDocument();
                htmlDoc.LoadHtml(rawHtml);
                
                var titleNode = htmlDoc.DocumentNode.SelectSingleNode("//title");
                return titleNode?.InnerText.Trim() ?? "Title not found";
            }
        }
    }
    
Step 2: Integrate the Library with Your AWS Lambda Function
  1. Create your Lambda project: Spin up an AWS Lambda project in your IDE, targeting a .NET Core version supported by Lambda (like .NET 6 or .NET 8—check AWS docs for the latest supported runtimes).
  2. Reference your class library: Right-click the Lambda project > Add > Project Reference, then select your .NET Standard class library.
  3. Call the library from Lambda: Update your Lambda handler to use the reusable logic:
    using Amazon.Lambda.Core;
    using LambdaReusableLogic;
    
    [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
    
    namespace MyLambdaFunction
    {
        public class Function
        {
            public string Handler(string inputHtml, ILambdaContext context)
            {
                var processor = new HtmlContentProcessor();
                return processor.GetPageTitle(inputHtml);
            }
        }
    }
    
Key Gotchas to Avoid
  • Dependency packaging: When deploying Lambda, use the dotnet lambda package command instead of manual zipping—it ensures all dependencies (including HTML Agility Pack) are correctly included in the deployment bundle.
  • Skip the console app as a reuse layer: If you want to test your library locally, create a console app that references the class library (instead of putting logic in the console project itself). This keeps your core logic isolated and reusable.
  • Check runtime compatibility: Confirm that your class library’s .NET Standard version matches what Lambda’s runtime supports. .NET Standard 2.0 works with all modern Lambda .NET runtimes, so it’s a safe bet.
Quick Local Test with a Console App

If you want to validate your library logic before deploying to Lambda:

  1. Create a .NET Core console project
  2. Reference your .NET Standard class library
  3. Add this to Program.cs:
    using LambdaReusableLogic;
    
    var testHtml = "<html><head><title>My Test Page</title></head></html>";
    var processor = new HtmlContentProcessor();
    Console.WriteLine($"Extracted Title: {processor.GetPageTitle(testHtml)}");
    

Run it, and you’ll quickly see if your logic works as expected.

内容的提问来源于stack exchange,提问作者LandonC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:38:58