.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.
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.
- 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.
- 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 - 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"; } } }
- 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).
- Reference your class library: Right-click the Lambda project > Add > Project Reference, then select your .NET Standard class library.
- 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); } } }
- Dependency packaging: When deploying Lambda, use the
dotnet lambda packagecommand 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.
If you want to validate your library logic before deploying to Lambda:
- Create a .NET Core console project
- Reference your .NET Standard class library
- 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

