{"record":{"id":"e4ccd410f4f216b5","repo":"microsoft/aspire","slug":"an-association-named-associationname-already-exists-in","errorCode":null,"errorMessage":"An association named '{associationName}' already exists in Network Security Perimeter '{nsp.Resource.Name}'.","messagePattern":"An association named '(.+?)' already exists in Network Security Perimeter '(.+?)'\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.Network/AzureNetworkSecurityPerimeterExtensions.cs","lineNumber":148,"sourceCode":"    /// storage.WithNetworkSecurityPerimeter(nsp);\n    /// keyVault.WithNetworkSecurityPerimeter(nsp, NetworkSecurityPerimeterAssociationAccessMode.Learning);\n    /// </code>\n    /// </example>\n    [AspireExport(\"associateWithNetworkSecurityPerimeter\", MethodName = \"withNetworkSecurityPerimeter\")]\n    public static IResourceBuilder<T> WithNetworkSecurityPerimeter<T>(\n        this IResourceBuilder<T> target,\n        IResourceBuilder<AzureNetworkSecurityPerimeterResource> nsp,\n        NetworkSecurityPerimeterAssociationAccessMode accessMode = NetworkSecurityPerimeterAssociationAccessMode.Enforced,\n        string? associationName = null) where T : IResource, IAzureNspAssociationTarget\n    {\n        ArgumentNullException.ThrowIfNull(target);\n        ArgumentNullException.ThrowIfNull(nsp);\n\n        associationName ??= $\"{target.Resource.Name}-assoc\";\n\n        if (nsp.Resource.Associations.Any(a => string.Equals(a.Name, associationName, StringComparison.OrdinalIgnoreCase)))\n        {\n            throw new ArgumentException(\n                $\"An association named '{associationName}' already exists in Network Security Perimeter '{nsp.Resource.Name}'.\",\n                nameof(associationName));\n        }\n\n        nsp.Resource.Associations.Add(new AzureNetworkSecurityPerimeterResource.NspAssociationConfig(\n            associationName,\n            target.Resource.Id,\n            accessMode));\n\n        return target;\n    }\n\n    private static void ConfigureNetworkSecurityPerimeter(AzureResourceInfrastructure infra)\n    {\n        var azureResource = (AzureNetworkSecurityPerimeterResource)infra.AspireResource;\n\n        var nsp = AzureProvisioningResource.CreateExistingOrNewProvisionableResource(infra,\n            (identifier, name) =>","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.Network/AzureNetworkSecurityPerimeterExtensions.cs#L130-L166","documentation":"WithNetworkSecurityPerimeter associates a target resource with an NSP and takes an association name (defaulting to '<target>-assoc'). Association names must be unique within the perimeter, so adding an association whose name already exists throws ArgumentException (case-insensitive comparison).","triggerScenarios":"Calling WithNetworkSecurityPerimeter for a second target whose generated or explicit associationName matches an existing association in the same NSP — e.g. two resources with the same name, or the same target associated twice.","commonSituations":"Reusing a constant associationName across multiple WithNetworkSecurityPerimeter calls, associating the same resource to a perimeter twice, or two resources whose names collide producing the same default '<target>-assoc' name.","solutions":["Pass an explicit, unique associationName for each WithNetworkSecurityPerimeter call.","Ensure the same target is not associated with the perimeter twice.","If resource names could collide, build association names that include a discriminator (subscription/scope or index).","Check nsp.Resource.Associations before adding when names are computed dynamically."],"exampleFix":"// before\nnsp.WithNetworkSecurityPerimeter(target, \"my-assoc\");\nnsp.WithNetworkSecurityPerimeter(other, \"my-assoc\");\n// after\nnsp.WithNetworkSecurityPerimeter(target, $\"{target.Resource.Name}-assoc\");\nnsp.WithNetworkSecurityPerimeter(other, $\"{other.Resource.Name}-assoc\");","handlingStrategy":"validation","validationCode":"var name = associationName ?? $\"{target.Resource.Name}-assoc\";\nif (nsp.Resource.Associations.Any(a => string.Equals(a.Name, name, StringComparison.OrdinalIgnoreCase)))\n    throw new ArgumentException($\"Association '{name}' already exists in NSP '{nsp.Resource.Name}'.\");","typeGuard":null,"tryCatchPattern":"try { nsp.WithNetworkSecurityPerimeter(target); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"already exists\")) { /* association already configured; skip or pass a unique name */ }","preventionTips":["Pass explicit unique associationName values when associating multiple targets.","Avoid associating the same target with a perimeter twice.","Guard against target resource-name collisions when relying on the default '<target>-assoc' name.","Check nsp.Resource.Associations before programmatic association additions."],"tags":["aspire","azure-network","nsp","duplicate-name"],"backgroundTag":"file-already-exists","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}