Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions src/Aspire.Hosting.Azure.Provisioning/AzureProvisioner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ private async Task ProvisionAzureComponents(IConfiguration configuration, IHostE

tasks.Add(task);

c.TryGetName(out var name);
componentNameToStorageAccountMap.Remove(name!);
componentNameToStorageAccountMap.Remove(c.Name);
}

if (c is AzureServiceBusComponent serviceBus)
Expand All @@ -173,8 +172,7 @@ private async Task ProvisionAzureComponents(IConfiguration configuration, IHostE

tasks.Add(task);

c.TryGetName(out var name);
componentNameToServiceBusNamespaceMap.Remove(name!);
componentNameToServiceBusNamespaceMap.Remove(c.Name);
}

if (c is AzureKeyVaultComponent keyVault)
Expand All @@ -190,8 +188,7 @@ private async Task ProvisionAzureComponents(IConfiguration configuration, IHostE

tasks.Add(task);

c.TryGetName(out var name);
componentNameToKeyVaultMap.Remove(name!);
componentNameToKeyVaultMap.Remove(c.Name);
}
}

Expand Down Expand Up @@ -230,8 +227,7 @@ private async Task CreateKeyVaultAsync(
Guid principalId,
CancellationToken cancellationToken)
{
keyVault.TryGetName(out var name);
componentNameToKeyVaultMap.TryGetValue(name!, out var keyVaultResource);
componentNameToKeyVaultMap.TryGetValue(keyVault.Name, out var keyVaultResource);

if (keyVaultResource is null)
{
Expand All @@ -247,7 +243,7 @@ private async Task CreateKeyVaultAsync(
EnableRbacAuthorization = true
};
var parameters = new KeyVaultCreateOrUpdateContent(location, properties);
parameters.Tags.Add("aspire-component-name", name);
parameters.Tags.Add("aspire-component-name", keyVault.Name);

var operation = await keyVaults.CreateOrUpdateAsync(WaitUntil.Completed, vaultName, parameters, cancellationToken).ConfigureAwait(false);
keyVaultResource = operation.Value;
Expand All @@ -274,8 +270,7 @@ private async Task CreateServiceBusAsync(
Guid principalId,
CancellationToken cancellationToken)
{
component.TryGetName(out var name);
componentNameToServiceBusNamespaceMap.TryGetValue(name!, out var serviceBusNamespace);
componentNameToServiceBusNamespaceMap.TryGetValue(component.Name, out var serviceBusNamespace);

if (serviceBusNamespace is null)
{
Expand All @@ -285,7 +280,7 @@ private async Task CreateServiceBusAsync(
logger.LogInformation("Creating service bus namespace {namespace} in {location}...", namespaceName, location);

var parameters = new ServiceBusNamespaceData(location);
parameters.Tags.Add("aspire-component-name", name);
parameters.Tags.Add("aspire-component-name", component.Name);

// Now we can create a storage account with defined account name and parameters
var operation = await serviceBusNamespaces.CreateOrUpdateAsync(WaitUntil.Completed, namespaceName, parameters, cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -367,8 +362,7 @@ private async Task CreateStorageAccountAsync(
Guid principalId,
CancellationToken cancellationToken)
{
component.TryGetName(out var name);
componentNameToStorageAccountMap.TryGetValue(name!, out var storageAccount);
componentNameToStorageAccountMap.TryGetValue(component.Name, out var storageAccount);

if (storageAccount is null)
{
Expand All @@ -381,7 +375,7 @@ private async Task CreateStorageAccountAsync(
var sku = new StorageSku(StorageSkuName.StandardGrs);
var kind = StorageKind.Storage;
var parameters = new StorageAccountCreateOrUpdateContent(sku, kind, location);
parameters.Tags.Add("aspire-component-name", name);
parameters.Tags.Add("aspire-component-name", component.Name);

// Now we can create a storage account with defined account name and parameters
var accountCreateOperation = await storageAccounts.CreateOrUpdateAsync(WaitUntil.Completed, accountName, parameters, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ public Task BeforeStartAsync(DistributedApplicationModel appModel, CancellationT
}
}));

component.Annotations.Add(new NameAnnotation { Name = sidecarOptions?.AppId ?? "Unknown" });
component.Annotations.AddRange(ports.Select(port => new ServiceBindingAnnotation(ProtocolType.Tcp, name: port.Key, port: port.Value.Port)));

// NOTE: Telemetry is enabled by default.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public abstract class DistributedApplicationComponent(string name) : IDistribute

private string DebuggerToString()
{
DistributedApplicationComponentExtensions.TryGetName(this, out var name);
return $@"Type = {GetType().Name}, Name = ""{name}""";
return $@"Type = {GetType().Name}, Name = ""{Name}""";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ namespace Aspire.Hosting.ApplicationModel;

public static class DistributedApplicationComponentExtensions
{
public static bool TryGetName(this IDistributedApplicationComponent component, [NotNullWhen(true)] out string? name)
{
var result = component.TryGetLastAnnotation<NameAnnotation>(out var nameAnnotation);
name = nameAnnotation?.Name;
return result;
}

public static bool TryGetLastAnnotation<T>(this IDistributedApplicationComponent component, [NotNullWhen(true)] out T? annotation) where T : IDistributedApplicationComponentAnnotation
{
if (component.Annotations.OfType<T>().LastOrDefault() is { } lastAnnotation)
Expand Down Expand Up @@ -77,10 +70,6 @@ public static bool TryGetContainerImageName(this IDistributedApplicationComponen
imageName = null;
return false;
}
public static bool HasName(this IDistributedApplicationComponent component, string name)
{
return component.TryGetName(out var componentName) && componentName == name;
}

public static int GetReplicaCount(this IDistributedApplicationComponent component)
{
Expand Down
12 changes: 0 additions & 12 deletions src/Aspire.Hosting/ApplicationModel/NameAnnotation.cs

This file was deleted.

10 changes: 1 addition & 9 deletions src/Aspire.Hosting/ComponentBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ public static IDistributedApplicationComponentBuilder<T> WithEnvironment<T>(this
return builder.WithAnnotation(new EnvironmentCallbackAnnotation(name, () => value ?? string.Empty));
}

public static IDistributedApplicationComponentBuilder<T> WithName<T>(this IDistributedApplicationComponentBuilder<T> builder, string name) where T : IDistributedApplicationComponent
{
return builder.WithAnnotation(new NameAnnotation { Name = name });
}

public static IDistributedApplicationComponentBuilder<T> WithEnvironment<T>(this IDistributedApplicationComponentBuilder<T> builder, string name, Func<string> callback) where T : IDistributedApplicationComponentWithEnvironment
{
return builder.WithAnnotation(new EnvironmentCallbackAnnotation(name, callback));
Expand All @@ -42,10 +37,7 @@ private static Action<EnvironmentCallbackContext> CreateServiceReferenceEnvironm
{
return (context) =>
{
if (!serviceReferencesAnnotation.Component.TryGetName(out var name))
{
throw new InvalidOperationException("When referencing component as an endpoint you must specify a name using WithName(string) on the referenced project.");
}
var name = serviceReferencesAnnotation.Component.Name;

var allocatedEndPoints = serviceReferencesAnnotation.Component.Annotations
.OfType<AllocatedEndpointAnnotation>()
Expand Down
47 changes: 2 additions & 45 deletions src/Aspire.Hosting/Dcp/ApplicationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,7 @@ private void PrepareContainers()
throw new InvalidOperationException();
}

var computedContainerName = container.TryGetName(out var specifiedContainerName) ? specifiedContainerName : containerImageName;
// TODO: the image name is really not the best name for Container object; we should use a "service name" or "component name"
var ctr = Container.Create(computedContainerName, containerImageName);
var ctr = Container.Create(container.Name, containerImageName);

if (container.TryGetVolumeMounts(out var volumeMounts))
{
Expand Down Expand Up @@ -630,48 +628,7 @@ public void Dispose()
private static string GetObjectNameForComponent(IDistributedApplicationComponent component, string suffix = "")
{
string maybeWithSuffix(string s) => string.IsNullOrWhiteSpace(suffix) ? s : $"{s}_{suffix}";

if (component.TryGetName(out var name))
{
return maybeWithSuffix(name);
}

switch (component)
{
case ContainerComponent:
if (!component.TryGetContainerImageName(out var imageName))
{
throw new ArgumentException("The container component has no name and no image information."); // Should never happen.
}

if (Rules.IsValidObjectName(imageName))
{
return maybeWithSuffix(imageName);
}
else
{
throw new ArgumentException($"Could not determine a good name for container component using image '{imageName}'; use WithName() on the container to fix this issue.");
}

case ProjectComponent:
if (!component.TryGetLastAnnotation<IServiceMetadata>(out var projectMetadata))
{
throw new ArgumentException("The project component has no name and no project metadata"); // Should never happen.
}

// TODO: the assembly name is really not the best name for Executable object (should use project name probably).
if (Rules.IsValidObjectName(projectMetadata.AssemblyName))
{
return maybeWithSuffix(projectMetadata.AssemblyName);
}
else
{
throw new ArgumentException($"Could not determine a good name for project component with assembly name '{projectMetadata.AssemblyName}'; use WithName() on the project to fix this issue.");
}

default:
throw new ArgumentException($"Could not determine a good name for component of type {component.GetType().Name}");
}
return maybeWithSuffix(component.Name);
}

private static string GenerateUniqueServiceName(List<string> serviceNames, string candidateName)
Expand Down
1 change: 0 additions & 1 deletion src/Aspire.Hosting/DistributedApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public IDistributedApplicationComponentBuilder<T> AddComponent<T>(T component) w
{
Components.Add(component);
var builder = new DistributedApplicationComponentBuilder<T>(this, component);
builder.WithName(component.Name); // TODO: Remove when fully transitioned Name away from annotation.
componentBuilders.Add(component, builder);
return builder;
}
Expand Down
7 changes: 1 addition & 6 deletions src/Aspire.Hosting/Publishing/ManifestPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@ private void WriteComponents(DistributedApplicationModel model, Utf8JsonWriter j

private void WriteComponent(IDistributedApplicationComponent component, Utf8JsonWriter jsonWriter)
{
if (!component.TryGetName(out var componentName))
{
throw new DistributedApplicationException("Component did not have name!");
}

jsonWriter.WriteStartObject(componentName);
jsonWriter.WriteStartObject(component.Name);

// First see if the component has a callback annotation with overrides the behavior for rendering
// out the JSON. If so use that callback, otherwise use the fallback logic that we have.
Expand Down