我正在尝试为 Windows Azure 创建一个在启动时加载自定义 EXE 的 Worker Role 项目。我改编了来自 David Chou 的 Jetty 服务器示例的代码,这里可以找到:
http://blogs.msdn.com/b/dachou/archive/2010/03/21/run-java-with-jetty-in-windows-azure.aspx
使用 Visual Web Developer 2010,我为 Visual C# 创建了一个新的 Cloud 项目,并放入了他的 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”文件夹在我通过搜索 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”文件夹文件也没有被复制。我仍然无法弄清楚为什么。
——罗施勒