先尝试将项目上传到 blob 存储是个好主意,但不幸的是,无论如何,这正是 Visual Studio 在幕后为您所做的事情。正如其他地方所指出的那样,进行部署的大部分时间不是上传本身,而是所有更新域的停止和启动。
如果您只是在开发环境中运行此站点,那么我所知道的加速它的唯一方法就是只运行一个实例。如果这是现场环境,那么……对不起,我认为你不走运。
所以我不必部署到云来测试微小的更改,我发现很好的方法是对站点进行工程设计,使其在本地 IIS 中运行时就像任何其他 MVC 站点一样工作。
这项工作的最大障碍是您在云配置中的设置。我们解决这个问题的方法是复制云配置中的所有设置,并将它们放在 appSettings 中的 web.config 中。然后,不要使用RoleEnvironment.GetConfigurationSettingValue(),而是创建一个您调用的包装类。这个包装类检查RoleEnvironment.IsAvailable 看它是否在Azure Fabric 中运行,如果是,它调用上面通常的配置函数,如果不是,它调用WebConfigurationManager.AppSettings[]。
围绕获取配置设置更改事件,您还需要做一些其他事情,希望您可以从下面的代码中弄清楚:
public class SmartConfigurationManager
{
private static bool _addConfigChangeEvents;
private static string _configName;
private static Func<string, bool> _configSetter;
public static bool AddConfigChangeEvents
{
get { return _addConfigChangeEvents; }
set
{
_addConfigChangeEvents = value;
if (value)
{
RoleEnvironment.Changing += RoleEnvironmentChanging;
}
else
{
RoleEnvironment.Changing -= RoleEnvironmentChanging;
}
}
}
public static string Setting(string configName)
{
if (RoleEnvironment.IsAvailable)
{
return RoleEnvironment.GetConfigurationSettingValue(configName);
}
return WebConfigurationManager.AppSettings[configName];
}
public static Action<string, Func<string, bool>> GetConfigurationSettingPublisher()
{
if (RoleEnvironment.IsAvailable)
{
return AzureSettingsGet;
}
return WebAppSettingsGet;
}
public static void WebAppSettingsGet(string configName, Func<string, bool> configSetter)
{
configSetter(WebConfigurationManager.AppSettings[configName]);
}
public static void AzureSettingsGet(string configName, Func<string, bool> configSetter)
{
// We have to store these to be used in the RoleEnvironment Changed handler
_configName = configName;
_configSetter = configSetter;
// Provide the configSetter with the initial value
configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
if (AddConfigChangeEvents)
{
RoleEnvironment.Changed += RoleEnvironmentChanged;
}
}
private static void RoleEnvironmentChanged(object anotherSender, RoleEnvironmentChangedEventArgs arg)
{
if ((arg.Changes.OfType<RoleEnvironmentConfigurationSettingChange>().Any(change => change.ConfigurationSettingName == _configName)))
{
if ((_configSetter(RoleEnvironment.GetConfigurationSettingValue(_configName))))
{
RoleEnvironment.RequestRecycle();
}
}
}
private static void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
{
// If a configuration setting is changing
if ((e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange)))
{
// Set e.Cancel to true to restart this role instance
e.Cancel = true;
}
}
}