目的是在端口80上的云服务网站上建立通配符绑定。这是在本地调用WCF服务。 我找到了在网站开头运行一些脚本的解决方案Running scripts from a Windows Azure role’s OnStart method 当我尝试实现这一点时,云服务无法启动:回收(等待角色启动...系统启动任务正在运行。
我已将以下内容添加到ServiceDefinition.csdef中:
<WebRole name="ESUITE" vmsize="Small">
<Runtime executionContext="elevated" />
...
<ConfigurationSettings>
<Setting name="RoleStartupScripts" />
</ConfigurationSettings>
</WebRole>
ServiceConfiguration.Cloud.cscfg:
<ConfigurationSettings>
<Setting name="RoleStartupScripts" value="Startup\startup.cmd" />
</ConfigurationSettings>
STARTUP.CMD:
powershell -command "set-executionpolicy Unrestricted"
powershell -File binding.ps1
exit
binding.ps1:
import-module WebAdministration
Get-WebSite "*ESUITE*" | Foreach-Object {
$site = $_;
$siteref = "IIS:/Sites/" + $site.Name;
New-ItemProperty $siteref -name bindings -value @{protocol="http";bindingInformation="*:80:"}
}
实现RoleEntryPoint的类:
public abstract class StartupScriptRoleEntryPoint : RoleEntryPoint
{
public override bool OnStart()
{
bool returnValue = false;
try
{
var startupScripts = CloudConfigurationManager.GetSetting("RoleStartupScripts");
if (!String.IsNullOrEmpty(startupScripts))
{
var scriptList = startupScripts.Split(';');
foreach (var script in scriptList)
{
bool waitForExit = false;
string scriptCommandLine = script;
if (script.EndsWith("!"))
{
scriptCommandLine = script.Substring(0, script.Length - 1);
waitForExit = true;
}
var args = CmdLineToArgvW.SplitArgs(scriptCommandLine);
var processStartInfo = new ProcessStartInfo()
{
FileName = args[0],
Arguments = string.Join(" ", args.Skip(1).ToArray()),
WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory,
UseShellExecute = true,
};
var process = Process.Start(processStartInfo);
if (waitForExit)
{
process.WaitForExit();
}
}
}
}
catch (Exception ex)
{
Trace.TraceError(ex.Message);
}
finally
{
returnValue = base.OnStart();
}
return returnValue;
}
}
我找不到导致网站未正确创建/启动的问题。