在 WebRole 中调用时,Environment.GetEnvironmentVariable(“RoleRoot”) 返回 null

发布于 2024-11-09 21:36:02 字数 620 浏览 0 评论 0原文

我有一个方法(在单独的类库中),由 WebRole 和 WorkerRole 调用。此方法包含文件的路径,使用 Environment.GetEnvironmentVariable("RoleRoot") 返回该路径,如下所示:

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

当我从 WorkerRole 调用此方法时,路径会正常返回。但是当我从 WebRole 调用它时,我得到 null

有什么想法吗?

编辑:我正在使用 APNS-Sharp 向 iOS 发送推送消息,它需要 .p12 证书才能工作。目前,我的类库的根目录中有 .p12(由 WebRole 和 WorkerRole 调用)。但重点是:为什么当我从 WebRole 调用时 RoleRoot 返回 null,而当我从 WorkerRole 调用时返回路径?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何以畏孤独 2024-11-16 21:36:02

RoleRoot 对于 WebRole 返回 false,因为 WebRole 使用 IIS,就像普通网站一样。这就是为什么很难从 WebRole 获取环境变量。

为了正确获取路径,我必须使用经典的 Server.MapPath 并引用 bin 文件夹,而不是 approot

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

对于 WorkerRole 没有任何更改:

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

此外,我发现Azure不导入p12证书。我必须将其转换为另一种格式,我认为这对我不起作用。因此,最好的选择是将它们放在应用程序的根目录中,并将其构建操作标记为内容

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.

欢你一世 2024-11-16 21:36:02

我从 webrole 尝试过,它对我有用。我将其放置在 Web 角色的 OnStart() 代码中,该代码由 WaIISHost 调用

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文