6

我正在实现组合的 web/worker 角色场景,如此处所述您只需将以下内容添加到您的 worker 角色中:

public override void Run()
{
    // This is a sample worker implementation. Replace with your logic.
    Trace.WriteLine("WorkerRole1 entry point called", "Information");
    while (true)
    {
        Thread.Sleep(10000);
        Trace.WriteLine("Working", "Information");
    }
}

如帖子评论中所述,问题在于该工作进程无法读取 web.config,因此您必须添加 app.config。还需要注意的是 app.config 不会自动部署。

所以我的问题是如何配置我的项目以便部署 app.config?

我已将 app.config 添加到我的项目中,将构建操作设置为“内容”和“始终复制”

这在模拟器中运行良好,但在部署到 Azure 时就不行了。

注意:我注意到在模拟器中创建了 projectname.dll.config,但在部署到 Azure 时没有。我正在使用 VS2010、Windows Azure 工具 2011

我知道有些人会建议改用 .cscfg 文件,但我的许多组件都从 web.config/app.config 获取它们的设置:Elmah、瞬态故障处理客户端、诊断、电子邮件等...

4

3 回答 3

3

请仔细阅读这篇博文。它详细解释了带有完整 IIS 的 Windows Azure Web 角色中发生的事情。

您需要做的是添加一个WaIISHost.exe.config文件(复制到输出 = 始终复制)。并将您需要的所有配置放入该文件中。这是因为,您的代码 (RoleEntryPoint) 位于 WaIISHost.exe 进程中,而不是您的 pdojectName.dll 进程中。

于 2012-08-30T19:28:09.600 回答
3

对于使用 Azure SDK 1.8 并使用发布从 Visual Studio 部署 Web Worker 角色的我来说,我必须包含一个名为 . ProjectName.Dll.config 与我的设置。在 Windows azure 中运行时,Web 角色不会获取 app.config 中的配置。并且app.config文件并没有转换成ProjectName.Dll.config并自动添加到部署包的bin文件夹中,所以你必须手动创建它并设置它总是复制。

于 2013-04-01T20:33:54.040 回答
2

我正在使用 Azure SDK 2.0 和 OS Family 3,对此我感到非常困惑。因此,我创建了一个 MVC 4.0 网站,其中包含各种答案中建议的所有 4 个配置文件。那是:

  • 网页配置
  • 应用程序配置
  • WaIISHost.exe.config
  • [程序集名称].dll.config

除了 Web.config 之外的所有内容都设置为“如果较新则复制”。

在我写的配置文件中:

  <appSettings>
    <add key="AppSettingFile" value="[NameOfConfigFile]"/>
  </appSettings>

在 WebRole.cs 我有以下代码:

    public class WebRole : RoleEntryPoint
    {
        public override void Run()
        {
            string appSetting = ConfigurationManager.AppSettings["AppSettingFile"] ?? "No config file found";
            Trace.TraceInformation("Config file: " + appSetting);

            while (true)
            {
                ...
            }
        }
    }

使用 4 个 .config 文件部署时的结果: "Config file: App.config"。所以 App.config 一定是答案,对吧?

错误的!仅使用 Web.config 和 App.config 部署时的结果: "Config file: No config file found"。嗯很奇怪。

使用 Web.config、App.config 和 [AssemblyName].dll.config 部署时的结果: "Config file: [AssemblyName].dll.config"。所以 [AssemblyName].dll.config 一定是答案,对吧?

错误的!仅使用 Web.config 和 [AssemblyName].dll.config 部署时的结果: "Config file: No config file found"。哇!

仅使用 Web.config 和 WaIISHost.exe.config 部署时的结果: "Config file: No config file found"

使用 Web.config、App.config 和 WaIISHost.exe.config 部署时的结果: "Config file: No config file found"。哇!

所以我的结论是你需要有 3 或 4 个配置文件才能配置 Web 项目的 Worker 角色。

这显然是一个错误。就我个人而言,我认为 MS 的意图是从 WaIISHost.exe.config 更改为 App.config(通常与 Worker Roles 和 .NET 保持一致)。但 App.config 仅在所有 4 个 .config 文件都存在时使用。

所以现在我有 Web.config 以及 App.config 和 [AssemblyName].dll.config,它们包含完全相同的内容。

希望随着 Azure SDK 2.x 的发展,我们只能使用 App.config 和 Web.config。

于 2013-06-02T16:09:11.333 回答