git-ecosystem/git-credential-manager · error · InvalidOperationException
Host name and/or port is invalid.
Error message
Host name and/or port is invalid.
What it means
UriHelpers.IsVisualStudioComHost(GitRequest) throws this InvalidOperationException when request.TryGetHostAndPort fails, i.e. the request does not carry a parsable host (and required port) so the library cannot decide whether the remote is a legacy visualstudio.com host. It is a guard ensuring host classification never runs against missing host data.
Solutions
- Inspect the remote URL: git remote -v; set a fully qualified absolute URL such as https://org.visualstudio.com/Project/_git/repo.
- Ensure the URL includes a scheme (https://) and a host name; relative, file, or scp-like remotes cannot be classified.
- If calling the API directly, construct the GitRequest with a valid absolute URI and a host/port that TryGetHostAndPort can extract.
- Check the request's environment/arguments: a missing or malformed remote URL passed via env vars (e.g. GCM behaviors driven by remote URL) will fail host extraction.
Example fix
// before
var request = new GitRequest(); // no remote URL -> no host
bool isVsCom = UriHelpers.IsVisualStudioComHost(request);
// after
var request = new GitRequest();
request.RemoteUrl = new Uri("https://org.visualstudio.com/Project/_git/repo");
bool isVsCom = UriHelpers.IsVisualStudioComHost(request); Defensive patterns
Strategy: validation
Validate before calling
if (!Uri.TryCreate(remoteUrl, UriKind.Absolute, out var uri) || string.IsNullOrEmpty(uri.Host))
throw new ArgumentException($"Remote URL '{remoteUrl}' must be absolute with a valid host before host classification.");
bool isVsCom = uri.Host.EndsWith(".visualstudio.com", StringComparison.OrdinalIgnoreCase); Type guard
static bool HasParsableHost(GitRequest request) =>
request?.RemoteUrl is Uri u && Uri.CheckHostName(u.Host) != UriHostNameType.Unknown; Try / catch
try { return UriHelpers.IsVisualStudioComHost(request); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Host name and/or port is invalid"))
{
// request lacks a usable host — treat as 'not visualstudio.com' and fix the URL upstream
return false;
} Prevention
- Always configure remotes as absolute https URLs with scheme and host.
- Call the string-host overload IsVisualStudioComHost(host) only after verifying TryGetHostAndPort/Uri parsing succeeded.
- Reject relative, file, or scp-like remote URLs early in your tooling before invoking GCM host helpers.
- Log the request's remote URL when this error occurs to spot blank/malformed remote config quickly.
When it happens
Trigger: Calling IsVisualStudioComHost(request) (directly or via IsAzureDevOpsHost(request)/CreateOrganizationUri) with a GitRequest whose remote URL cannot yield a host name — empty or relative URL, malformed URI, or an address where the required port is absent per TryGetHostAndPort semantics.
Common situations: A remote URL configured without a scheme/host (e.g. git remote set-url origin /path/to/repo or ssh-style scp syntax) that GCM later tries to classify; programmatically constructing a GitRequest with an empty RequestUri; tests/tools passing a partially populated request.
Understand the failure class
Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.
Related errors
- Host is not Azure DevOps.
- Cannot determine the organization name for this…
- Failed to parse host name and/or port
- Provided URI ' ' is not a valid Azure DevOps hostname
- Failed to create PAT
AI-assisted analysis of git-ecosystem/git-credential-manager@e8ce762cd0 (2026-09-11).
Data as JSON: /api/errors/679c0cfd04c5a867.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.AzureRepos/UriHelpers.cs:46
return basePath + '/' + path;
}
}
return basePath + path;
}
/// <summary>
/// Check if the hostname is the legacy Azure DevOps hostname (*.visualstudio.com).
/// </summary>
/// <param name="request">Git query arguments.</param>
/// <returns>True if the hostname is the legacy Azure DevOps host, false otherwise.</returns>
public static bool IsVisualStudioComHost(GitRequest request)
{
EnsureArgument.NotNull(request, nameof(request));
if (!request.TryGetHostAndPort(out string hostName, out _))
{
throw new InvalidOperationException("Host name and/or port is invalid.");
}
return IsVisualStudioComHost(hostName);
}
/// <summary>
/// Check if the hostname is the legacy Azure DevOps hostname (*.visualstudio.com).
/// </summary>
/// <param name="host">Hostname to check.</param>
/// <returns>True if the hostname is the legacy Azure DevOps host, false otherwise.</returns>
public static bool IsVisualStudioComHost(string host)
{
return host != null &&
host.EndsWith(AzureDevOpsConstants.VstsHostSuffix, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Check if the hostname is the new Azure DevOps hostname (dev.azure.com).View on GitHub (pinned to e8ce762cd0)