7

I have a method (in a separated class library) which is called by a WebRole and a WorkerRole. This method contains the path of a file, which 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 a WorkerRole the path is returned normally. But when I call it from a WebRole I get null.

Any ideas?

EDIT: I am using APNS-Sharp to send push messages to iOS and it requires a .p12 certificate in order to work. Currently I have the .p12 in the root of my class library (which is called by both WebRole and WorkerRole). But the point is: Why RoleRoot returns null when I call it from a WebRole but returns the path when I call from a WorkerRole?

Davidson Sousa
  • 1,353
  • 1
  • 14
  • 34
  • Are you sure you need to locate files in `RoleRoot`? It seems to be a wrong idea for me. – Snowbear May 25 '11 at 21:48
  • I need to load a certificate. Would blob be a better idea? – Davidson Sousa May 25 '11 at 21:55
  • can you make it a little bit more clear what are you going to do with this file? Who will read it and who will write it? Probably all you need is this: http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleenvironment.getlocalresource.aspx – Snowbear May 25 '11 at 22:02
  • I have just edited the question. :) – Davidson Sousa May 25 '11 at 22:16
  • regarding your question quick googling results in this: http://blogs.msdn.com/b/avkashchauhan/archive/2011/05/25/role-environment-variables-in-full-iis-web-role-and-in-hwc-web-role.aspx – Snowbear May 26 '11 at 08:40
  • Thanks! That helped me to understand my problem and I even came with the solution. Can you post as answer? :-) – Davidson Sousa May 26 '11 at 23:32

3 Answers3

10

RoleRoot returns false for WebRole because the WebRole uses IIS, just like a normal website. That's why it's difficult to get Environment Variables from a WebRole.

In order to get the path properly I had to use the classic Server.MapPath and reference the bin folder, instead of approot:

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

For the 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 doesn't import p12 certificates. I would have to transform it into another format, which I don't believe would work for me. So, the best option is to place them on the root of the application and mark its Build Action to Content.

Davidson Sousa
  • 1,353
  • 1
  • 14
  • 34
1

I tried from webrole and it works for me. I place it at the OnStart() code of the web role, which is called by WaIISHost

Lingxiao
  • 11
  • 1
0

If you want to load a certificate, you could try the advice in http://blogs.msdn.com/b/jnak/archive/2010/01/29/installing-certificates-in-windows-azure-vms.aspx

Linked from How do I import a public certificate to Windows Azure?

Community
  • 1
  • 1
Stuart
  • 66,722
  • 7
  • 114
  • 165