Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Amazon.Lambda.Annotations How to use ILambdaContext in dependecy injection container? #1535

Answered by normj
Breno709 asked this question in Q&A
Discussion options

I can use ILambdaContext by injecting it directly at the endpoint handlers, as explained in documentation:

[LambdaFunction(MemorySize = 512, Timeout = 55)]
[HttpApi(LambdaHttpMethod.Get, "/add/{x}/{y}")]
public int Add(int x, int y, ILambdaContext context)
{
 context.Logger.LogInformation($"{x} plus {y} is {x + y}");
 return x + y;
}

but I would like to inject it in others services through DI container, since I'm using depency injection. But I didn't realize how to achieve this goal. I can't find a way to register ILambdaContext at DI container.
Am I erroneously thinking about the use of ILambdaContext? Is it that a missing feature? Or there's a way to achieve that somehow?

You must be logged in to vote

You are correct we are manually injected ILambdaContext for the method. We could add it to the DI container as well if we see the user is using DI. My worry though is this is DI resolution for services at the constructor for the type with the method would fail. There is no ILambdaContext at the point the constructor is called. It would only be good for services using the FromService attribute on parameters of the LambdaFunction. Is that what you would expect to happen? Given those constraints would you still use it?

@github-actions proposed-answer

Replies: 2 comments 4 replies

Comment options

You are correct we are manually injected ILambdaContext for the method. We could add it to the DI container as well if we see the user is using DI. My worry though is this is DI resolution for services at the constructor for the type with the method would fail. There is no ILambdaContext at the point the constructor is called. It would only be good for services using the FromService attribute on parameters of the LambdaFunction. Is that what you would expect to happen? Given those constraints would you still use it?

@github-actions proposed-answer

You must be logged in to vote
4 replies
Comment options

Comment options

I think I need this in order to inject the current context request/trace id into a dependency of my function call (e.g. an internal API client). for example:

Functions.cs is the handler class, which expects an IMyApiService as a constructor argument.
In Startup.cs I set up the transient of IMyApiService, and the transient of Functions. I want to attach the current request id as a tracing header to my API service client, which I could do within each function call using its ILambdaContext but I'd rather do it once.

Comment options

What to do exactly this too.

Comment options

@normj I think that solution is acceptable, we need the request id in several methods and our current solution it to pass it as a string parameter in all the call chain, if we could do [FromServices] ILambdaContext in the methods that need the request id would help us a lot.
It's worth mentioning that we are not using Lambda Annotations, it's a regular old lambda.

Answer selected by ashishdhingra
Comment options

Hello! Reopening this discussion to make it searchable.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /