为什么 GetEnvironmentVariable() 在 Windows Azure 中为我的 RoleRoot 返回错误的目录?

发布于 2024-11-18 13:49:36 字数 3538 浏览 7 评论 0 原文

我正在尝试为 Windows Azure 创建一个工作角色项目,该项目在启动时加载自定义 EXE。我改编了 David Chou 的 Jetty 服务器示例中的代码:

http://blogs.msdn.com/b/dachou/archive/2010/03/21/run-java-with-jetty-in-windows-azure.aspx

使用可视化 Web Developer 2010,我为 Visual C# 创建了一个新的云项目,并放入了此处所示的 Worker Role 类代码:

namespace WorkerRole1
{
    public class WorkerRole : RoleEntryPoint
    {
        public override void Run()
        {
            string response = "";
            string S = "";

            try
            {
                System.IO.StreamReader sr;
                string port = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["HttpIn"].IPEndpoint.Port.ToString();
                string roleRoot = Environment.GetEnvironmentVariable("RoleRoot");

                S = "roleRoot is: " + roleRoot;
                Trace.TraceInformation(S);

                // string myAzureAppHome = roleRoot + @"\approot\app";
                string myAzureAppHome = System.IO.Path.Combine(roleRoot + @"\", @"approot\");

                S = "myAzureAppHome is: " + myAzureAppHome;
                Trace.TraceInformation(S);


                // string jreHome = roleRoot + @"\approot\app\jre6";
                Process proc = new Process();
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;
                //proc.StartInfo.FileName = String.Format("\"{0}\\bin\\java.exe\"", jreHome);
                // proc.StartInfo.Arguments = String.Format("-Djetty.port={0} -Djetty.home=\"{1}\" -jar \"{1}\\start.jar\"", port, myAzureAppHome);
                proc.StartInfo.FileName = String.Format("\"{0}\\myAzureAppPRJ.exe\"", myAzureAppHome);

                S = "Attempting to run file: " + proc.StartInfo.FileName;
                Trace.TraceInformation(S);

                proc.EnableRaisingEvents = false;
                proc.Start();
                sr = proc.StandardOutput;
                response = sr.ReadToEnd();
            }
            catch (Exception ex)
            {
                response = ex.Message;
                Trace.TraceError(response);
            } 
        }

        public override bool OnStart()
        {
            // Set the maximum number of concurrent connections 
            ServicePointManager.DefaultConnectionLimit = 12;

            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

            return base.OnStart();
        }
    }
}

问题是我为 RoleRoot 返回的目录与我的“app”支持文件所在的位置不匹配当我针对存储模拟器运行项目时出现。 “app”文件夹最终位于我通过搜索 WindowsAzureProject2 目录树找到的目录中:

C:\Users\mycomp\Documents\Visual Studio 2010\Projects\WindowsAzureProject2\WorkerRole1\app

但 GetEnvironmentVariable("RoleRoot") 报告以下内容作为我的 Trace 语句转储的 RoleRoot 目录:

C:\Users\mycomp\documents\visual studio 2010\Projects\WindowsAzureProject2\WindowsAzureProject2\bin\Debug\WindowsAzureProject2.csx\roles\WorkerRole1

这当然会导致调用 proc.Start() 时出现文件未找到异常,因为自定义 EXE 文件是在前一个目录中,而不是在后者中。

谁能告诉我为什么 roleRoot 路径似乎被破坏了,我能做些什么来解决这个问题?请注意,我知道在分配给 proc.StartInfo.FileName 的 EXE 之前存在双反斜杠问题(Visual Web Developer C# 与 Visual Studio Pro C# 不同吗?),但修复该问题不会改变我遇到的路径问题因为 GetEnvironmentVariable("RoleRoot") 返回的目录完全不包含我的“app”目录。

更新:经过更多阅读后,我发现真正发生的情况是“approot”目录没有被创建,我的“app”文件夹文件也没有被复制。我还是不明白为什么。

——罗施勒

I am trying to create a Worker Role project for Windows Azure that loads a custom EXE upon startup. I adapted the code from David Chou's Jetty server example found here:

http://blogs.msdn.com/b/dachou/archive/2010/03/21/run-java-with-jetty-in-windows-azure.aspx

Using Visual Web Developer 2010, I created a new Cloud project for Visual C# and dropped in his Worker Role class code shown here:

namespace WorkerRole1
{
    public class WorkerRole : RoleEntryPoint
    {
        public override void Run()
        {
            string response = "";
            string S = "";

            try
            {
                System.IO.StreamReader sr;
                string port = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["HttpIn"].IPEndpoint.Port.ToString();
                string roleRoot = Environment.GetEnvironmentVariable("RoleRoot");

                S = "roleRoot is: " + roleRoot;
                Trace.TraceInformation(S);

                // string myAzureAppHome = roleRoot + @"\approot\app";
                string myAzureAppHome = System.IO.Path.Combine(roleRoot + @"\", @"approot\");

                S = "myAzureAppHome is: " + myAzureAppHome;
                Trace.TraceInformation(S);


                // string jreHome = roleRoot + @"\approot\app\jre6";
                Process proc = new Process();
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;
                //proc.StartInfo.FileName = String.Format("\"{0}\\bin\\java.exe\"", jreHome);
                // proc.StartInfo.Arguments = String.Format("-Djetty.port={0} -Djetty.home=\"{1}\" -jar \"{1}\\start.jar\"", port, myAzureAppHome);
                proc.StartInfo.FileName = String.Format("\"{0}\\myAzureAppPRJ.exe\"", myAzureAppHome);

                S = "Attempting to run file: " + proc.StartInfo.FileName;
                Trace.TraceInformation(S);

                proc.EnableRaisingEvents = false;
                proc.Start();
                sr = proc.StandardOutput;
                response = sr.ReadToEnd();
            }
            catch (Exception ex)
            {
                response = ex.Message;
                Trace.TraceError(response);
            } 
        }

        public override bool OnStart()
        {
            // Set the maximum number of concurrent connections 
            ServicePointManager.DefaultConnectionLimit = 12;

            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

            return base.OnStart();
        }
    }
}

The problem is that the directory I am getting back for the RoleRoot does not match where my "app" support files are showing up when I run the project against the storage emulator. The "app" folder ends up in this directory which I found by searching the WindowsAzureProject2 directory tree:

C:\Users\mycomp\Documents\Visual Studio 2010\Projects\WindowsAzureProject2\WorkerRole1\app

But GetEnvironmentVariable("RoleRoot") reports the following as the RoleRoot directory as dumped by my Trace statement:

C:\Users\mycomp\documents\visual studio 2010\Projects\WindowsAzureProject2\WindowsAzureProject2\bin\Debug\WindowsAzureProject2.csx\roles\WorkerRole1

Which of course leads to a file not found Exception when proc.Start() is called because the custom EXE file is in the former directory, and not the latter.

Can anyone tell my why the roleRoot path appears to be botched and what I can do to fix this? Note, I'm aware of the double backslash problem before the EXE in my assignment to proc.StartInfo.FileName (is Visual Web Developer C# different from Visual Studio Pro C#?), but fixing that would not change the path problem I am having since the directory being returned by GetEnvironmentVariable("RoleRoot") flat out does not contain my "app" directory.

UPDATE: After doing some more reading it appears to me that what's really happening is that the "approot" directory is not being created and my "app" folder files are not being copied over either. I still can't figure out why.

-- roschler

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

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

发布评论

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

评论(1

等风来 2024-11-25 13:49:36

看来您没有将应用程序目录中的文件添加到 Visual Studio 项目中。确保它们已添加并将其“复制到输出目录”属性设置为“始终复制”(或“如果较新则复制”)。然后它们应该像您期望的那样最终位于 %RoleRoot% 下。

Looks like you didn't add the files in the app directory to the Visual Studio project. Make sure they're added and have their "Copy to Output Directory" property set to "Copy Always" (or "Copy if Newer"). Then they should end up under %RoleRoot% like you expect.

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