-
Notifications
You must be signed in to change notification settings - Fork 494
-
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?
Beta Was this translation helpful? Give feedback.
All reactions
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
-
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
Beta Was this translation helpful? Give feedback.
All reactions
-
Also refer blog post How To Set Up Dependency Injection in Lambda Functions Using Annotations Framework for reference.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1 -
👀 1
-
What to do exactly this too.
Beta Was this translation helpful? Give feedback.
All reactions
-
@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.
Beta Was this translation helpful? Give feedback.
All reactions
-
Hello! Reopening this discussion to make it searchable.
Beta Was this translation helpful? Give feedback.