几个月后,我找到了解决这个问题的方法。根本问题是 Azure 在每个角色实例上为网站命名不同;该网站的名称不是“默认网站”,而是类似NewOrbit.ExVerifier.Web_IN_0_Web
(为您的 Web 项目指定一个名称空间 NewOrbit.ExVerifier.Web)。工作流使用网站名称作为用于计算实例密钥的算法的一部分,因此出现了问题。
解决方案很简单,就是在角色启动期间重命名网站,以便在所有实例上都调用相同的名称。解决根本问题而不是处理后果,如此明显,我第一次从未见过它。
以下是您可以执行此操作的方法(大致基于此:http://blogs.msdn.com/b/tomholl/archive/2011/06/28/hosting-services-with-was-and-iis-on-windows-azure.aspx)
配置 powershell 以具有提升的访问权限,以便您可以在配置 IIS 后进行更改:
In ServiceDefinition.csdef
添加启动任务:
<ServiceDefinition name="WasInAzure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WebRole name="WebRole1">
...
<Startup>
<Task commandLine="setup\startup.cmd" executionContext="elevated" />
</Startup>
</WebRole>
</ServiceDefinition>
Setup\Startup.cmd
应该有这样的内容:
powershell -command "set-executionpolicy Unrestricted" >> out.txt
配置 OnStar 角色以具有管理员权限
In ServiceDefinition.csdef
添加这个:
<ServiceDefinition name="WasInAzure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WebRole name="WebRole1">
...
<Runtime executionContext="elevated" />
</WebRole>
</ServiceDefinition>
创建 powershell 脚本来重命名网站
创建一个setup\RoleStart.ps1
file:
write-host "Begin RoleStart.ps1"
import-module WebAdministration
$siteName = "*" + $args[0] + "*"
Get-WebSite $siteName | Foreach-Object {
$site = $_;
$siteref = "IIS:/Sites/" + $site.Name;
try {
Rename-Item $siteref 'MyWebSite'
write-host $siteName + " was renamed"
}
catch
{
write-host "Failed to rename " + $siteName + " : " + $error[0]
}
}
write-host "End RoleStart.ps1"
(将 MyWebSite 替换为您希望在所有服务器上调用该网站的任何内容)。
在角色启动时运行 RoleStart.ps1:
在网站项目的根目录中创建或编辑 WebRole.cs 并添加以下代码:
public class WebRole : RoleEntryPoint
{
public override bool OnStart()
{
var startInfo = new ProcessStartInfo()
{
FileName = "powershell.exe",
Arguments = @".\setup\rolestart.ps1",
RedirectStandardOutput = true,
UseShellExecute=false,
};
var writer = new StreamWriter("out.txt");
var process = Process.Start(startInfo);
process.WaitForExit();
writer.Write(process.StandardOutput.ReadToEnd());
writer.Close();
return base.OnStart();
}
}
应该就是这样。如果您启动多个 Web 角色实例并使用 RDP 连接到它们,您现在应该能够看到该网站在所有实例上的调用方式相同,因此工作流持久性有效。