Skip to content

Instantly share code, notes, and snippets.

@bronumski
Created June 19, 2014 11:25
Show Gist options
  • Select an option

  • Save bronumski/cd8eda47279b1009e488 to your computer and use it in GitHub Desktop.

Select an option

Save bronumski/cd8eda47279b1009e488 to your computer and use it in GitHub Desktop.
Creates or gets a uri for a service or unique name with a random free port.
static class UriManager
{
private static readonly IDictionary<string, Uri> Uris = new Dictionary<string, Uri>();
public static Uri GetUriForService<TService>()
{
return GetUri(typeof (TService).Name);
}
public static Uri GetUri(string serviceName)
{
if (Uris.ContainsKey(serviceName))
{
return Uris[serviceName];
}
var uri = new UriBuilder(Uri.UriSchemeHttp, IPAddress.Loopback.ToString(), TcpPort.NextFreePort()).Uri;
Uris.Add(serviceName, uri);
return uri;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment