2

This page describes how to assign host name to role instance through cscfg. How do i access it in my app? The following approach is not working since RoleInstance does not have a VMName member.

foreach (RoleInstance instance in RoleEnvironment.Roles["MyRole"].Instances) {
    Console.WriteLine(instance.VMName);
}
kaiz.net
  • 1,984
  • 3
  • 23
  • 31

2 Answers2

0

When you are inside your Virtual Machine, you can use any .NET API to get the host name in your code i.e.:

Dns.GetHostName();

The reason you may change your Windows Azure VM host name is that in most cases the VM hostname is set as RD00*** so if you have multiple instances all the host names will be different and not easy to remember and manage. So to keep all the host name easy to remember you will use the following property so all instances related to same role will have name as vm-name##, this way you can easily remember the host name and manage them properly.

<Role name="<role-name>" vmName=“<vm-name>”>

So to change hostname you can use above Configuration settings but to get hostname you can just use standard .net API and it will work.

AvkashChauhan
  • 20,495
  • 3
  • 34
  • 65
0

As @AvkashChauhan pointed out, you should be able to retrieve a hostname through the Dns class. I'm curious, though, why you need the hostname. If it's for communication between role instances, you actually don't need the hostname. Rather, you can query the role instance endpoints (assuming you set one up as an internal endpoint), and then for each instance, you can request the ip address and port, which would allow you to establish a direct connection to any given instance. So, modifying the code you had:

foreach (RoleInstance instance in RoleEnvironment.Roles["MyRole"].Instances) {
   writeline(InstanceEndpoints["myendpoint"].IPEndpoint);

You may, indeed, need hostname for something, and if so, just ignore this answer. But... if what you're looking for is a way to make tcp/http/udp connections between role instances, this is a pretty simple way to retrieve all the ip/port combinations for your role instances.

David Makogon
  • 69,407
  • 21
  • 141
  • 189