Environment.GetEnvironmentVariable ("RoleRoot"), returning null when called in WebRole

I have a method (in a separate class library) that is called by WebRole and WorkerRole. This method contains the path to the file that is returned using Environment.GetEnvironmentVariable("RoleRoot") as follows:

 private string FooPath() { string appRoot = Environment.GetEnvironmentVariable("RoleRoot"); return Path.Combine(appRoot + @"\", @"approot\file.foo"); } 

When I call this method from WorkerRole, the path returns normally. But when I call this from WebRole, I get null .

Any ideas?

EDIT: I use APNS-Sharp to send push messages in iOS and require a .p12 certificate to work. I currently have .p12 at the root of my class library (which is called by both WebRole and WorkerRole). But the point is: why does RoleRoot return null when I call it from WebRole, but it returns the path when I call from WorkerRole?

+6
source share
3 answers

RoleRoot returns false for WebRole because WebRole uses IIS like a regular website. Therefore, it is difficult to get environment variables from WebRole.

To get the path correctly, I had to use the classic version of Server.MapPath and refer to the bin folder instead of approot :

 private string FooPathWebRole() { string appRoot = HttpContext.Current.Server.MapPath(@"~\"); return Path.Combine(appRoot + @"\", @"bin\file.foo"); } 

For WorkerRole, nothing has changed:

 private string FooPathWorkerRole() { string appRoot = Environment.GetEnvironmentVariable("RoleRoot"); return Path.Combine(appRoot + @"\", @"approot\file.foo"); } 

In addition, I found out that Azure does not import p12 certificates. I would have to convert it to another format, which, in my opinion, would not work for me. So, the best option is to put them in the root application and mark it. Building an action on the Content .

+10
source

I tried with webrole and it works for me. I put it in the OnStart () code of the web role that is called by WaIISHost

+1
source

Source: https://habr.com/ru/post/889044/


All Articles