【问题标题】:Can I set the Diagnostic connection string for Windows Azure WebRole from code?我可以从代码中为 Windows Azure WebRole 设置诊断连接字符串吗?
【发布时间】:2013-09-08 00:47:08
【问题描述】:

我们在 webrole 中托管 3party 站点,为了限制它们访问存储容器,我需要从代码中设置连接字符串,而不是在服务配置中设置连接字符串?

这可能吗?

根据回答我遇到了问题。

        DiagnosticMonitorConfiguration dmConfig = DiagnosticMonitor.GetDefaultInitialConfiguration();
        DiagnosticMonitor.StartWithConnectionString(conn, dmConfig);

这会将配置重置为默认值并覆盖使用云服务部署的内容。我假设在使用 StartWithConnectionString 时,您不能使用他们在 Visual Studio 中添加的支持来设置这些东西。

【问题讨论】:

  • 不确定我是否完全理解此声明This resets the configuration to defaults and overrides the stuff that was deployed with the cloud service.。你能解释一下吗?
  • 如果存储中有诊断设置,它将覆盖它。但这是正确的设计。我想出了如何读取当前配置并使用它。
  • @PoulK.Sørensen 您写道:“我想出了如何读取当前配置并使用它” - 如果可能,您能否分享一下您是如何做到的?谢谢。
  • 抱歉,已经有几年了,在我以前的工作中,所以无法检查代码我是如何做到的。

标签: azure


【解决方案1】:

是的,我想你可以。看看DiagnosticMonitor.StartWithConnectionString 方法。你会在你的 WebRole 的 OnStart() 方法中做这样的事情:

        DiagnosticMonitorConfiguration dmConfig = DiagnosticMonitor.GetDefaultInitialConfiguration();
        DiagnosticMonitor.StartWithConnectionString("DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=accountkey", dmConfig);
        return base.OnStart();

但是,我不建议在代码本身中对连接字符串进行硬编码。而是从某个数据库中获取它。

【讨论】:

  • Np,我在 RoleSettings 中有连接字符串。它只是编码:) 使用这种方法,我可以在代码中将诊断连接字符串设置为空吗?我可以在 Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString 设置中添加编码的连接字符串并从那里读取它并对其进行解码然后 StartWithConnectionString 吗? (我问是因为我在某处读到诊断实际上是在 OnStart 之前开始的)。如果你不知道,我会测试一下。
  • 啊,我明白了。出于某种原因,我想到了别的东西。我认为应该是可行的。您可以加密连接字符串,然后在调用 DiagnosticsMonitor.StartWithConnectionString 之前将其解密并将其传递给函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-10
相关资源
最近更新 更多