Commit 3d48e200 authored by DeepakVerma's avatar DeepakVerma

Default ClientName to Roleinstance ID for a web/worker role to help identify...

Default ClientName to Roleinstance ID for a web/worker role to help identify where a client connection is coming from, further helps to identify connectivity problems.
parent 29b5cc18
......@@ -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]
public void ReadConfigWithConfigDisabled()
{
......
......@@ -5,6 +5,7 @@
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
......@@ -161,7 +162,7 @@ public static string TryNormalize(string value)
/// <summary>
/// The client name to use for all connections
/// </summary>
public string ClientName { get { return clientName; } set { clientName = value; } }
public string ClientName { get { return clientName ?? (clientName = AzureRoleInstanceId); } set { clientName = value; } }
/// <summary>
/// The number of times to repeat the initial connect cycle if no servers respond promptly
......@@ -665,6 +666,46 @@ private bool IsAzureEndpoint()
return result;
}
internal static string AzureRoleInstanceId = TryGetAzureRoleInstanceIdNoThrow();
/// <summary>
/// Tries to get the Roleinstance Id if Microsoft.WindowsAzure.ServiceRuntime is loaded.
/// In case of any failure, swallows the exception and returns null
/// </summary>
private 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;
}
private string InferSslHostFromEndpoints() {
var dnsEndpoints = endpoints.Select(endpoint => endpoint as DnsEndPoint);
string dnsHost = dnsEndpoints.FirstOrDefault()?.Host;
......
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