【问题标题】:Options to manage local vs cloud development configuration settings in Visual Studio在 Visual Studio 中管理本地与云开发配置设置的选项
【发布时间】:2013-01-11 09:22:27
【问题描述】:

我们将在未来使用 Windows Azure 网站来满足我们的基本托管需求。由于云与本地有非常具体的配置设置,您如何管理这些设置?举个例子:

本地开发服务器:

string path = AppSettings["StoragePath"];

<add key="StoragePath" value="c:\temp" />

Windows Azure:

string path = AppSettings["StoragePath"];

<add key="StoragePath" value="xyz" />

您是否在每次发布之前手动更改配置文件中的 StoragePathOR 代码中是否有一些可以完成的操作,例如:

<add key="LocalStoragePath" value="c:\temp" />
<add key="BlobStoragePath" value="xyz" />    

string path;
if (Azure)
{
   path = AppSettings["BlobStoragePath"];
}
else
{
   path = AppSettings["LocalStoragePath"];
}

如果后者可以,如何判断环境是否为Windows Azure?

【问题讨论】:

  • 为什么不能使用serviceconfiguration.local和serviceconfiguration.cloud的configurationsettings?
  • @allen 我对此持开放态度,只是我没有意识到这一点。 serviceconfiguration 会完全取代 AppSettings 吗?我还应该补充一点,我们正在部署简单的网站,而不是虚拟机,如果这有影响的话。

标签: c# .net azure configuration


【解决方案1】:

我通常会创建一个新的构建配置(称为 Azure)。

然后在 web.config 中创建您的密钥..

<add key="LocalStoragePath" value="c:\blah" />
<add key="AzureStoragePath" value="xyz" />

在你的代码中写:

#if CONFIG == "Azure"
  public const String storageKey = "AzureStoragePath";
#endif CONFIG == "Debug"
  public const String storageKey = "LocalStoragePath";
#endif

并使用它:

String path = AppSettings[storageKey];

【讨论】:

    【解决方案2】:
    public interface IConfigurationProvider { }
    
    public class AzureConfigurationProvider : IConfigurationProvider { }
    
    public class LocalConfigurationProvider : IConfigurationProvider { }
    
    public static class ConfigurationProviderFactory
    {
        private static bool _isAzure = Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.IsAvailable;
    
        private static Lazy<IConfigurationProvider> _provider = Lazy<IConfigurationProvider>(GetProvider);
    
        private static IConfigurationProvider GetProvider()
        {
             return _isAzure ?
                 new AzureConfigurationProvider() :
                 new LocalConfigurationProvider();
        }
    
        public static IConfigurationProvider Instance
        {
            get { return _provider.Value; }
        }
    }
    

    【讨论】:

      【解决方案3】:

      假设您在 VS 2010 或 VS2012 中使用 Web Publishing 功能的latest version,您可以通过发布配置文件和 web.config 转换相当轻松地完成此操作。

      首先,创建您的发布配置文件(右键单击项目,选择发布,浏览对话框)。这将是进行各种配置更改的默认位置,例如连接字符串。

      然后,右键单击为您的发布配置文件创建的 .pubxml 文件,应该有一个添加转换的选项。这将添加一个新的 web..config,它应该出现在 web.Debug.config/web.Release.config 旁边。

      在该文件中,您可以为要更改的应用设置添加转换。当您使用该配置文件发布时,将应用转换值;本地开发仍将使用您想要的任何值。

      【讨论】:

        猜你喜欢
        • 2011-07-17
        • 1970-01-01
        • 1970-01-01
        • 2019-04-10
        • 2011-01-03
        • 2011-04-01
        • 1970-01-01
        • 2015-08-20
        • 2012-02-11
        相关资源
        最近更新 更多