Can an Azure WorkerRole detect where it's located?
What I mean by my question is: is it possible to detect which datacenter the worker role is deployed in? I'm trying to programatically figure out开发者_StackOverflow社区 if my worker role is in a North American versus European Azure datacenter for the purpose of connecting to the database located in the same geography.
For example, is there an environment variable? Or maybe I could derive this information based on the public IP address? I'm willing to entertain any alternative.
The reason I'm asking this question is because I would like to deploy the exact same worker role in a NA datacenter and also deploy it in EU and eventually also in APAC but I want the role to connect to the closest database. I am trying to avoid having to manually specify the connection string in the .CFG because I am afraid one day someone will deploy the code to the EU and forget to adjust the connection string. My goal is to have all the connections strings to be present in the .CFG and my worker role to be smart enough to select the appropriate one based on its location.
Yes, but you would have to use the Service Management API. To do this without any configuration settings, you would first enumerate the hosted services and then for each one call for Get Hosted Services Properties (http://msdn.microsoft.com/en-us/library/ee460806.aspx). You would need to match the DeploymentId you have in your RoleEnvironment to the PrivateId in this response. That would tell you which hosted service you are running under. In that same response is the location.
Now, there is an even easier method. Just put the value in the ServiceConfiguration and have that differ by deployment!
精彩评论