Commit 6b747c59 authored by Marc Gravell's avatar Marc Gravell

Merge pull request #379 from deepakverma/clientname

Default ClientName to Roleinstance ID for a web/worker role 
parents 29b5cc18 18a7427e
...@@ -124,6 +124,28 @@ public void ClientName() ...@@ -124,6 +124,28 @@ public void ClientName()
} }
} }
[Test]
public void DefaultClientName()
{
using (var muxer = Create(allowAdmin: true))
{
Assert.AreEqual(Environment.MachineName, muxer.ClientName);
var conn = muxer.GetDatabase();
conn.Ping();
#if DEBUG
var name = GetServer(muxer).ClientGetName();
Assert.AreEqual(Environment.MachineName, name);
#endif
}
}
[Test]
public void TryGetAzureRoleInstanceIdNoThrow()
{
ConfigurationOptions config = new ConfigurationOptions();
Assert.IsNull(config.TryGetAzureRoleInstanceIdNoThrow());
}
[Test] [Test]
public void ReadConfigWithConfigDisabled() public void ReadConfigWithConfigDisabled()
{ {
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Reflection;
#if NET40 #if NET40
using Microsoft.Runtime.CompilerServices; using Microsoft.Runtime.CompilerServices;
#else #else
...@@ -91,7 +92,53 @@ public ServerCounters GetCounters() ...@@ -91,7 +92,53 @@ public ServerCounters GetCounters()
/// <summary> /// <summary>
/// Gets the client-name that will be used on all new connections /// Gets the client-name that will be used on all new connections
/// </summary> /// </summary>
public string ClientName => configuration.ClientName ?? Environment.GetEnvironmentVariable("ComputerName"); public string ClientName => configuration.ClientName ?? ConnectionMultiplexer.GetDefaultClientName();
private static string defaultClientName;
private static string GetDefaultClientName()
{
if (defaultClientName == null)
{
defaultClientName = TryGetAzureRoleInstanceIdNoThrow() ?? Environment.GetEnvironmentVariable("ComputerName");
}
return defaultClientName;
}
/// Tries to get the Roleinstance Id if Microsoft.WindowsAzure.ServiceRuntime is loaded.
/// In case of any failure, swallows the exception and returns null
internal static string TryGetAzureRoleInstanceIdNoThrow()
{
string roleInstanceId = null;
try
{
Assembly asm = null;
foreach (var asmb in AppDomain.CurrentDomain.GetAssemblies())
{
if (asmb.GetName().Name.Equals("Microsoft.WindowsAzure.ServiceRuntime"))
{
asm = asmb;
break;
}
}
if (asm == null)
return null;
var currentRoleInstanceProp = asm.GetType("Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment").GetProperty("CurrentRoleInstance");
var currentRoleInstanceId = currentRoleInstanceProp.GetValue(null, null);
roleInstanceId = currentRoleInstanceId.GetType().GetProperty("Id").GetValue(currentRoleInstanceId, null).ToString();
if (String.IsNullOrEmpty(roleInstanceId))
{
roleInstanceId = null;
}
}
catch (Exception)
{
//silently ignores the exception
roleInstanceId = null;
}
return roleInstanceId;
}
/// <summary> /// <summary>
/// Gets the configuration of the connection /// Gets the configuration of the connection
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment