Skip to content

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

Open
1 task done
SamVanhoutte opened this issue Apr 28, 2025 · 6 comments
Open
1 task done
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication

Comments

@SamVanhoutte
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

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)

bool isRunning = AspireHostContext.Current != null;

Additional context

No response

@github-actions github-actions bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Apr 28, 2025
@davidfowl
Copy link
Member

Do you really need this? Can you show some code that you can't make work by adding configuration?

@SamVanhoutte
Copy link
Author

SamVanhoutte commented Apr 28, 2025

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")));
}

@jack775544
Copy link

jack775544 commented Apr 28, 2025

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)}")

@davidfowl
Copy link
Member

Easier:

builder.AddProject<MyProject>("MyProject")
    .WithEnvironment("MyService_url", myService.GetEndpoint("http"));

@SamVanhoutte
Copy link
Author

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 AspireHostContext.IsRunningInAspireAppHost myself).
Even if there would just be a specific Environment Variable (like with Container Apps, you have 'CONTAINER_APP_HOSTNAME' , for example) that I could check for that I know will always be there, regardless of user requiring configuration.

@davidfowl
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication
Projects
None yet
Development

No branches or pull requests

3 participants