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

Annotations Framework - does this have access to an IHostEnvironment ? #1517

Answered by ashishdhingra
PureKrome asked this question in Q&A
Discussion options

👋🏻 G'Day team!

With the Annotations Framework, we can register services into the DI container.

e.g.

[LambdaStartup]
public class Startup
{
 public void ConfigureServices(IServiceCollection services)
 {
 // Prepare our configuration data.
 var builder = new ConfigurationBuilder()
 .SetBasePath(Directory.GetCurrentDirectory())
 .AddJsonFile("appsettings.json", optional: false)
 .AddJsonFile($"appsettings.{????}.json", optional: false)
 .AddEnvironmentVariables();
 var configuration = builder.Build();
 services.AddSingleton<IConfiguration>(configuration);
 services.AddSingleton<IDatabase>(implementationInstance =>
 {
 // Logic here
 });
 // Register more services, if required.
 }
}

But in the code above, I was hoping to access if the HostEnvironment that I'm familiar with in a normal ASP.NET Core API project : Development or Production (for example).

Is this available here, in Annotation Framework or even in Lambda's in general?

I understand that the value of the host environment is a number of Environment Variables (like ASPNETCORE_ENVIRONMENT) so I could probably try and manually see if that exists (not sure how?).

But yeah - is this possible or not?

Thank you kindly.

You must be logged in to vote

@PureKrome Good morning. Based on my understanding, the Lambda Annotations framework uses C# source generators to create the translation layer at compile time.

It uses FieldAndConstructor TT template to emit constructor for the generated type, where it includes a statement to invoke the Startup type's (class decorated with LambdaStartupAttribute) ConfigureServices(IServiceCollection) method.

My assumption is that during deployment, you could set the environment variable ASPNETCORE_ENVIRONMENT to appropriate value and should be able to read it in your code.
CC @normj

Thanks,
Ashish

Replies: 2 comments

Comment options

@PureKrome Good morning. Based on my understanding, the Lambda Annotations framework uses C# source generators to create the translation layer at compile time.

It uses FieldAndConstructor TT template to emit constructor for the generated type, where it includes a statement to invoke the Startup type's (class decorated with LambdaStartupAttribute) ConfigureServices(IServiceCollection) method.

My assumption is that during deployment, you could set the environment variable ASPNETCORE_ENVIRONMENT to appropriate value and should be able to read it in your code.
CC @normj

Thanks,
Ashish

You must be logged in to vote
0 replies
Answer selected by PureKrome
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 によって変換されたページ (->オリジナル) /