Skip to content

Commit 83fb710

Browse files
Update README (#2017)
1 parent 56145f3 commit 83fb710

File tree

1 file changed

+48
-15
lines changed
  • Libraries/src/Amazon.Lambda.Annotations

1 file changed

+48
-15
lines changed

Diff for: Libraries/src/Amazon.Lambda.Annotations/README.md

+48-15
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,37 @@ original declaration should be manually removed.
163163
## Dependency Injection integration
164164

165165
Lambda Annotations supports dependency injection. A class can be marked with a `LambdaStartup` attribute. The class will
166-
have a `ConfigureServices` method for configuring services.
166+
have a `ConfigureHostBuilder` method for configuring the host builder. `ConfigureHostBuilder` should return an implementation of `IHostApplicationBuilder`.
167167

168-
The services can be injected by either constructor injection or using the `FromServices` attribute on a method parameter of
168+
Services can be injected by either constructor injection or using the `FromServices` attribute on a method parameter of
169169
the function decorated with the `LambdaFunction` attribute.
170170

171171
Services injected via the constructor have a lifecycle for the length of the Lambda compute container. For each Lambda
172172
invocation a scope is created and the services injected using the `FromServices` attribute are created within the scope.
173173

174-
Example startup class:
174+
Example startup class using the recommended ConfigureHostBuilder:
175+
```csharp
176+
[LambdaStartup]
177+
public class Startup
178+
{
179+
public HostApplicationBuilder ConfigureHostBuilder()
180+
{
181+
var hostBuilder = new HostApplicationBuilder();
182+
183+
// Register services
184+
hostBuilder.Services.AddAWSService<Amazon.S3.IAmazonS3>();
185+
hostBuilder.Services.AddScoped<ITracker, DefaultTracker>();
186+
187+
// Add other configuration if needed
188+
hostBuilder.AddServiceDefaults();
189+
190+
return hostBuilder;
191+
}
192+
}
193+
```
194+
195+
For legacy support, you can still use the ConfigureServices method:
196+
175197
```csharp
176198
[LambdaStartup]
177199
public class Startup
@@ -372,12 +394,16 @@ Here the `ICalculatorService` is registered as a singleton service in the collec
372394
[LambdaStartup]
373395
public class Startup
374396
{
375-
376-
public void ConfigureServices(IServiceCollection services)
397+
public HostApplicationBuilder ConfigureHostBuilder()
377398
{
378-
services.AddSingleton<ICalculatorService, DefaultCalculatorService>();
399+
var hostBuilder = new HostApplicationBuilder();
400+
401+
hostBuilder.Services.AddSingleton<ICalculatorService, DefaultCalculatorService>();
402+
403+
return hostBuilder;
379404
}
380405
}
406+
381407
```
382408

383409
Since the `ICalculatorService` is registered as a singleton the service is injected into the Lambda function via the constructor.
@@ -487,15 +513,18 @@ public class Functions_Add_Generated
487513

488514
public Functions_Add_Generated()
489515
{
490-
var services = new ServiceCollection();
516+
var startup = new CloudCalculator.Startup();
517+
var hostBuilder = startup.ConfigureHostBuilder();
518+
491519

492520
// By default, Lambda function class is added to the service container using the singleton lifetime
493-
// To use a different lifetime, specify the lifetime in Startup.ConfigureServices(IServiceCollection) method.
494-
services.AddSingleton<Functions>();
521+
// To use a different lifetime, specify the lifetime in Startup.ConfigureHostBuilder() method.
522+
hostBuilder.Services.AddSingleton<Functions>();
495523

496-
var startup = new CloudCalculator.Startup();
497-
startup.ConfigureServices(services);
498-
serviceProvider = services.BuildServiceProvider();
524+
serviceProvider = hostBuilder.Services.BuildServiceProvider();
525+
526+
host = hostBuilder.Build();
527+
host.RunAsync();
499528
}
500529

501530
public Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyResponse Add(Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyRequest request, Amazon.Lambda.Core.ILambdaContext context)
@@ -579,16 +608,20 @@ service has state that should not be preserved per invocation.
579608
[LambdaStartup]
580609
public class Startup
581610
{
582-
public void ConfigureServices(IServiceCollection services)
611+
public HostApplicationBuilder ConfigureHostBuilder()
583612
{
613+
var hostBuilder = new HostApplicationBuilder();
614+
584615
// Using the AWSSDK.Extensions.NETCore.Setup package add the AWS SDK's S3 client
585-
services.AddAWSService<Amazon.S3.IAmazonS3>();
616+
hostBuilder.Services.AddAWSService<Amazon.S3.IAmazonS3>();
586617

587618
// Add service for handling image manipulation.
588619
// IImageServices is added as transient service so a new instance
589620
// is created for each Lambda invocation. This can be important if services
590621
// have state that should not be persisted per invocation.
591-
services.AddTransient<IImageServices, DefaultImageServices>();
622+
hostBuilder.Services.AddTransient<IImageServices, DefaultImageServices>();
623+
624+
return hostBuilder;
592625
}
593626
}
594627
```

0 commit comments

Comments
 (0)