microsoft / microsoft/aspire

Provide a way to detect if a service is running in the context of NET Aspire host

Open
#8,999 8 comments 0 reactions 0 assignees View on GitHub
area-app-model
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 12h
Merged PRs (30d)
201

Description

### Is there an existing issue for this?

- [x] 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)

```csharp
bool isRunning = AspireHostContext.Current != null;
```

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by reviewing how services are launched by the .NET Aspire host and how standalone execution is represented in Program.cs. Compare that with the available IServiceProvider services and determine where a supported host-context signal could be exposed. Done means applications can reliably distinguish Aspire-hosted from standalone execution without a manually added environment variable.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
cloud
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.