共用方式為


ResourceExtensions.GetEnvironmentVariableValuesAsync Method

Definition

Get the environment variables from the given resource.

public static System.Threading.Tasks.ValueTask<System.Collections.Generic.Dictionary<string,string>> GetEnvironmentVariableValuesAsync (this Aspire.Hosting.ApplicationModel.IResourceWithEnvironment resource, Aspire.Hosting.DistributedApplicationOperation applicationOperation = Aspire.Hosting.DistributedApplicationOperation.Run);
static member GetEnvironmentVariableValuesAsync : Aspire.Hosting.ApplicationModel.IResourceWithEnvironment * Aspire.Hosting.DistributedApplicationOperation -> System.Threading.Tasks.ValueTask<System.Collections.Generic.Dictionary<string, string>>
<Extension()>
Public Function GetEnvironmentVariableValuesAsync (resource As IResourceWithEnvironment, Optional applicationOperation As DistributedApplicationOperation = Aspire.Hosting.DistributedApplicationOperation.Run) As ValueTask(Of Dictionary(Of String, String))

Parameters

resource
IResourceWithEnvironment

The resource to get the environment variables from.

applicationOperation
DistributedApplicationOperation

The context in which the AppHost is being executed.

Returns

The environment variables retrieved from the resource.

Examples

Using GetEnvironmentVariableValuesAsync(IResourceWithEnvironment, DistributedApplicationOperation) inside a unit test to validate environment variable values.

var builder = DistributedApplication.CreateBuilder();
var container = builder.AddContainer("elasticsearch", "library/elasticsearch", "8.14.0")
 .WithEnvironment("discovery.type", "single-node")
 .WithEnvironment("xpack.security.enabled", "true");

var env = await container.Resource.GetEnvironmentVariableValuesAsync();

Assert.Collection(env,
    env =>
        {
            Assert.Equal("discovery.type", env.Key);
            Assert.Equal("single-node", env.Value);
        },
        env =>
        {
            Assert.Equal("xpack.security.enabled", env.Key);
            Assert.Equal("true", env.Value);
        });

Remarks

This method is useful when you want to make sure the environment variables are added properly to resources, mostly in test situations. This method has asynchronous behavior when applicationOperation is Run and environment variables were provided from IValueProvider otherwise it will be synchronous.

Applies to