-
Notifications
You must be signed in to change notification settings - Fork 602
Provide a way to detect if a service is running in the context of NET Aspire host #8999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Do you really need this? Can you show some code that you can't make work by adding configuration? |
It's for framework code (so where I want to avoid users having to add configuration for this). public Task<Uri> GetServiceUriAsync(string serviceName)
{
// This is where I would love to have this check implemented
var aspireHosted = AspireHostContext.IsRunningInAspireAppHost;
return Task.FromResult(aspireHosted
// We return the .NET Aspire url
? new Uri($"http://{serviceName}")
// We return the configured fallback url
: new Uri(configuration[$"{serviceName}_url"] ?? throw new ArgumentNullException($"{serviceName}_url")));
} |
Instead of using trying to detect the apphost, you should use the apphost to set the configuration instead. So your code would look something like this public Task<Uri> GetServiceUriAsync(string serviceName)
{
return Task.FromResult(new Uri(configuration[$"{serviceName}_url"] ?? throw new ArgumentNullException($"{serviceName}_url"));
} And in your apphost builder.AddProject<MyProject>("MyProject")
.WithEnvironment("MyService_url", $"{myService.GetEndpoint("http").Property(EndpointProperty.Url)}") |
Easier: builder.AddProject<MyProject>("MyProject")
.WithEnvironment("MyService_url", myService.GetEndpoint("http")); |
It's exactly this that I'd love to avoid (asking to users to pass this to tens of projects they add to the Host, whereas I was hoping to just get this functionality out of the box. (Happy to implement the |
I don't think you need it. You just need to do what @jack775544 said and make the app host model what the application needs (https://medium.com/@davidfowl/modeling-your-environment-with-aspire-24e986752485). If you think you need it then set an environment variable that you can check in your application. |
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
I am trying to find a way to detect if a service is running in the context of the .NET Aspire host, or if it's running standalone (as a console app, for example). This is needed to resolve service dependencies, for example (as url's may change).
However, the only way to work around it , as far as I can see it, is to add an Environment Variable in the Program.cs of the AspireHost project. Whereas, I'd love to get a better way to check for this.
I also tried to see if there were specific services injected in the
IServiceProvider
, but that was not the case as well.Describe the solution you'd like
I have not found a way, so it would be nice of there would be something available like the following. (where there is a static "context class" available for this)
Additional context
No response
The text was updated successfully, but these errors were encountered: