{"record":{"id":"f6572c564fcc38c4","repo":"microsoft/aspire","slug":"allocatedendpoint-must-use-the-same-network-as-the-networkid","errorCode":null,"errorMessage":"AllocatedEndpoint must use the same network as the networkId parameter","messagePattern":"AllocatedEndpoint must use the same network as the networkId parameter","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ApplicationModel/EndpointAnnotation.cs","lineNumber":446,"sourceCode":"        lock (_snapshots)\n        {\n            if (_snapshots.Any(s => s.NetworkID.Equals(networkId)))\n            {\n                return false;\n            }\n            _snapshots.Add(new NetworkEndpointSnapshot(snapshot, networkId));\n            return true;\n        }\n    }\n\n    /// <summary>\n    /// Adds or updates an AllocatedEndpoint value associated with a specific network in the snapshot list.\n    /// </summary>\n    public void AddOrUpdateAllocatedEndpoint(NetworkIdentifier networkId, AllocatedEndpoint endpoint)\n    {\n        if (endpoint.NetworkID != networkId)\n        {\n            throw new ArgumentException($\"AllocatedEndpoint must use the same network as the {nameof(networkId)} parameter\", nameof(endpoint));\n        }\n        var nes = GetSnapshotFor(networkId);\n        nes.Snapshot.SetValue(endpoint);\n    }\n\n    /// <summary>\n    /// Gets an AllocatedEndpoint for a given network ID, waiting for it to appear if it is not already present.\n    /// </summary>\n    public Task<AllocatedEndpoint> GetAllocatedEndpointAsync(NetworkIdentifier networkId, CancellationToken cancellationToken = default)\n    {\n        var nes = GetSnapshotFor(networkId);\n        return nes.Snapshot.GetValueAsync(cancellationToken);\n    }\n\n    internal bool TryGetAllocatedEndpoint(NetworkIdentifier networkId, [NotNullWhen(true)] out AllocatedEndpoint? endpoint)\n    {\n        endpoint = null;\n","sourceCodeStart":428,"sourceCodeEnd":464,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/EndpointAnnotation.cs#L428-L464","documentation":"AddOrUpdateAllocatedEndpoint stores an AllocatedEndpoint under a specific NetworkIdentifier bucket. To keep the snapshot list consistent, the endpoint's own NetworkID must equal the networkId parameter; otherwise endpoints would be filed under the wrong network. A mismatch throws ArgumentException on the endpoint parameter.","triggerScenarios":"Calling annotation.AddOrUpdateAllocatedEndpoint(netA, endpointForNetB) — typically passing a network identifier variable that differs from the one used when the endpoint was allocated/resolved.","commonSituations":"Looping over networks with the loop variable for the key but a stale/captured endpoint from a previous iteration; test fakes constructing endpoints with a hardcoded NetworkID; copying allocation logic between networks after a refactor added multi-network support.","solutions":["Pass an AllocatedEndpoint whose NetworkID equals the networkId argument (or construct one with that value).","Derive both from the same source: allocate the endpoint from the same NetworkIdentifier you pass in.","Guard the call: if (endpoint.NetworkID != networkId) fix or skip before calling."],"exampleFix":"// before\nannotation.AddOrUpdateAllocatedEndpoint(NetworkIdentifier.Localhost, endpointForDockerNetwork);\n// after\nvar endpoint = new AllocatedEndpoint(annotation, \"localhost\", port, NetworkIdentifier.Localhost);\nannotation.AddOrUpdateAllocatedEndpoint(NetworkIdentifier.Localhost, endpoint);","handlingStrategy":"validation","validationCode":"if (endpoint.NetworkID != networkId)\n    throw new ArgumentException($\"Endpoint network '{endpoint.NetworkID}' does not match target network '{networkId}'.\", nameof(endpoint));","typeGuard":null,"tryCatchPattern":"try\n{\n    annotation.AddOrUpdateAllocatedEndpoint(networkId, endpoint);\n}\ncatch (ArgumentException ex) when (ex.ParamName == nameof(endpoint))\n{\n    logger.LogError(ex, \"Endpoint network mismatch: {Message}\", ex.Message);\n    throw;\n}","preventionTips":["Construct the AllocatedEndpoint from the same NetworkIdentifier you pass to AddOrUpdateAllocatedEndpoint.","Avoid hardcoded NetworkID values in fakes/builders — derive them from the network parameter.","In loops over networks, build the endpoint inside the loop so it always matches the current identifier.","Add a unit test exercising multi-network allocation to catch captured-variable mismatches."],"tags":["dotnet","aspire","networking","endpoints"],"backgroundTag":"invalid-argument-value","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"}