简体   繁体   English

Azure辅助角色配置文件转换

[英]Azure Worker Role Config File Transformations

I've setup a new worker role and setup a couple of new config transforms for it via SlowCheetah. 我已经设置了一个新的worker角色,并通过SlowCheetah为它设置了几个新的配置转换。 When I build the project with one of the new configs selected, I do in fact see that configs folder get created underneath the \\bin folder as you would expect (for ex. \\bin\\Production). 当我使用所选的一个新配置构建项目时,我确实看到configs文件夹在\\ bin文件夹下创建,如您所期望的那样(对于例如\\ bin \\ Production)。

When I package a cloud service for deployment using one of the new configs, my web projects get their configs transformed appropriately but my worker role (which is just a library) does not even though I see underneath the \\bin folder an updated \\bin\\production. 当我使用其中一个新配置打包云服务进行部署时,我的Web项目会对其配置进行适当的转换但是我的工作者角色(这只是一个库)即使我在\\ bin文件夹下面看到一个更新的\\ bin \\生产。

It would appear the azure packaging tooling is ignoring the config set for the worker role library. 看来天蓝色的包装工具忽略了工作者角色库的配置集。 How can I get it to pick the config file from the appropriate the configuration? 如何从适当的配置中选择配置文件?

Yes, you can do this - and it is even very easy once you know how. 是的,你可以这样做 - 一旦你知道如何,它甚至很容易。
App.config is not transformed by design but fortunately the Azure Team made the build/deploy process very extensible exactly for these kinds of scenarios. App.config不是按设计转换的但幸运的是Azure团队使构建/部署过程非常可扩展,适用于这些场景。 What you need to do is reasonably well documented, though in a very roundabout way and most articles assume you are already familiar with MSBuild scripts and the like. 您需要做的事情已经得到了充分的记录,但是以非常迂回的方式,并且大多数文章假设您已经熟悉MSBuild脚本等。

Below you will find the lines you need to put into your project that will make this Just Work. 在下面,您将找到需要添加到项目中的行,这将使这个Just Work成为可能。 That should take no more than five minutes. 这应该不会超过五分钟。 Please note that this is not a hack - the whole Azure deploy process is designed to support this kind of thing. 请注意,这不是一个黑客 - 整个Azure部署过程旨在支持这种事情。

If you want to know more, there are some links to related articles at the bottom. 如果您想了解更多信息,可以在底部找到相关文章的链接。

Some conceptual points 一些概念点

  1. The recommended way to achieve this kind of thing in Azure is to not use Web.config and App.config but instead use the CloudConfigurationManager and use Role Settings. 在Azure中实现此类操作的推荐方法是使用Web.config和App.config,而是使用CloudConfigurationManager并使用角色设置。 However, sometimes that just isn't the right answer, usually because of built-in or 3rd party components that require *.config settings (smtp, wcf, elmah etc). 但是,有时候这不是正确的答案,通常是因为内置或第三方组件需要* .config设置(smtp,wcf,elmah等)。
  2. Web Config transformations is designed for transforming only the web.config. Web Config转换旨在仅转换web.config。 This means that app.config is not transformed by design . 这意味着app.config不会被设计转换。
  3. Web Config transformations is designed to only kick in when publishing so when you run locally, even in the Cloud Emulator, your Web.config won't be transformed. Web Config转换旨在仅在发布时启动,因此当您在本地运行时,即使在Cloud Emulator中,也不会转换Web.config。

The way we can solve this is by hooking into the build process for the Cloud project. 我们解决这个问题的方法是深入了解Cloud项目的构建过程。 When you deploy the project to Azure, the Cloud project will be built using a build process you can hook into. 将项目部署到Azure时,将使用您可以挂钩的构建过程构建Cloud项目。 In short, the cloud project builds the web and worker roles and puts them under the Obj folder under your cloud project. 简而言之,云项目构建Web和辅助角色,并将它们放在您的云项目下的Obj文件夹下。 It then runs a process that essentially zips all that up and finally put the result into the Bin folder. 然后它运行一个基本上拉上所有内容的进程,最后将结果放入Bin文件夹。 From there, the "zip" file and a configuration file is uploaded to Azure. 从那里,“zip”文件和配置文件上传到Azure。

The solution 解决方案

Is to manually edit your Cloud.csproj file (if you do it from within Visual Studio you need to unload the project first). 是手动编辑您的Cloud.csproj文件(如果您在Visual Studio中执行此操作,则需要先卸载项目)。 Then add this right above the closing </project> tag: 然后将其添加到结束</project>标记上方:

  <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v11.0\Web\Microsoft.Web.Publishing.targets" />
    <PropertyGroup>
      <WorkerRoleDir>$(IntermediateOutputPath)WorkerRole1\</WorkerRoleDir>
      <AppConfigOriginal>$(WorkerRoleDir)WorkerRole1.dll.config</AppConfigOriginal>
      <AppConfigTransformer>$(SolutionDir)WorkerRole1\App.$(Configuration).config</AppConfigTransformer>
      <AppConfigAfterTransformed>$(WorkerRoleDir)AfterTransformed.config</AppConfigAfterTransformed>
    </PropertyGroup>
    <Target Name="TransformAppConfig" AfterTargets="AfterPackageComputeService">
      <Message Text="Transforming $(AppConfigOriginal) via $(AppConfigTransformer) to $(AppConfigAfterTransformed)" />
      <TransformXml Source="$(AppConfigOriginal)" Transform="$(AppConfigTransformer)" Destination="$(AppConfigAfterTransformed)" />
      <Copy SourceFiles="$(AppConfigOriginal)" DestinationFiles="$(WorkerRoleDir)App.Config.Original" />
      <Copy SourceFiles="$(AppConfigAfterTransformed)" DestinationFiles="$(AppConfigOriginal)" />
    </Target>

Notes 笔记

  • There are a couple of hard-coded paths in there that you will have to modify. 那里有几条硬编码路径,你必须修改它们。 I am sure there is a way to make them soft but that requires more MSBuild skills than I have. 我确信有一种方法可以使它们柔软,但这需要比我更多的MSBuild技能。
  • The transformation will actually run when you deploy to your local Cloud Emulator, but it won't be used . 当您部署到本地Cloud Emulator时,转换将实际运行 ,但不会使用它 As such, the result is consistent with the behavior of Web.config which is also not transformed. 因此,结果与Web.config的行为一致,也没有转换。 But, if your transformation was to fail, you will get a build error even when just running in the Emulator. 但是,如果您的转换失败,即使只是在模拟器中运行,也会出现构建错误。
  • See also This other SO question 另见另外这个问题
  • An in-depth exploration 深入探索
  • Tom Hollanders highly linked article on deploying directly from MSBuild Tom Hollanders高度关联直接从MSBuild部署的文章

I find @Frans answer too complicated, and below is the code I found in the internet . 我发现@Frans的答案太复杂了,下面是我在互联网上找到的代码。 Given that you already have your app.config transformation set up and working, open your cloud project (.ccproj) in text editor, find this line: 鉴于您已经设置并运行了app.config转换,请在文本编辑器中打开您的云项目(.ccproj),找到以下行:

<Import Project="$(CloudExtensionsDir)Microsoft.WindowsAzure.targets" />

and insert the following after it: 并在其后插入以下内容:

  <!-- Get worker role transform start -->
  <Target Name="CopyWorkerRoleConfigurations" AfterTargets="CopyWorkerRoleFiles">
    <Copy SourceFiles="$(WorkerTargetDir)\YOUR-PROJECT-NAME.dll.config" DestinationFolder="$(IntermediateOutputPath)YOUR-PROJECT-NAME" OverwriteReadOnlyFiles="true" />
  </Target>
  <!-- Get worker role transform end -->

and replace YOUR-PROJECT-NAME with your worker project name. 并将YOUR-PROJECT-NAME替换为您的工作项目名称。

UPDATE UPDATE

I actually found a better way to do this (MSBuild 4+): script above will NOT work if you have more than 1 worker role with app.config transformations in your Azure project. 我实际上找到了一种更好的方法(MSBuild 4+):如果你的Azure项目中有app.config转换的工作角色超过1个,那么上面的脚本将无效。 Here is the more generalized way: 这是更通用的方式:

  <Target Name="CopyWorkerRoleConfigurations" AfterTargets="CopyWorkerRoleFiles">
    <PropertyGroup>
         <RootFolder>$([System.IO.Path]::GetDirectoryName($(MSBuildProjectDirectory)))</RootFolder>
    </PropertyGroup>

    <Copy SourceFiles="$(RootFolder)\%(ProjectName)\bin\$(Configuration)\%(EntryPoint).config" DestinationFolder="%(WorkerRoleReferences.OutputDir)" OverwriteReadOnlyFiles="true" />
  </Target>  

Make sure you Cloud Service configurations are set up. 确保已设置Cloud Service配置。 Right-click the cloud project and you should see the configurations you are trying to package. 右键单击云项目,您应该看到要尝试打包的配置。 For example, I rename and use Local, TEST & PROD configurations. 例如,我重命名并使用Local,TEST和PROD配置。 Your Cloud Service project should then contain 3 config files: 然后,您的Cloud Service项目应包含3个配置文件:

  • ServiceConfiguration.Local.cscfg ServiceConfiguration.Local.cscfg
  • ServiceConfiguration.PROD.cscfg ServiceConfiguration.PROD.cscfg
  • ServiceConfiguration.TEST.cscfg ServiceConfiguration.TEST.cscfg

Right-click the Cloud Service project and select "Package" and select the correct Service & Build configurations for your deployment. 右键单击Cloud Service项目并选择“Package”,然后为部署选择正确的Service&Build配置。

Note: app.configs aren't transformed in the build process unless you add an MS - See this SO answer for a technique to do a transform in the build process. 注意:除非您添加MS,否则不会在构建过程中转换app.configs - 请参阅此SO答案,了解在构建过程中进行转换的技术。 Howerver, for cloud deployments, you are better off using ServiceConfiguration.*.cscfg files in combination with CloudConfigurationManager.GetSetting("settingsKey") - CloudConfigurationManager.GetSetting was added in SDK 1.7 - if in Role, it gets the value from ServiceConfig else get from web.config/app.config appSettings Howerver,对于云部署,最好使用ServiceConfiguration。*。cscfg文件与CloudConfigurationManager.GetSetting("settingsKey")结合使用 - 在SDK 1.7中添加了CloudConfigurationManager.GetSetting - 如果在Role中,它从ServiceConfig获取其他值获取来自web.config / app.config appSettings

There is one trick. 有一个技巧。 Perhaps someone will come in handy. 也许有人会派上用场。 Azure Worker Role builder includes files that marked as "Copy". “Azure工作者角色”构建器包含标记为“复制”的文件。 But app.config will be transform in WorkerRole1.dll.config (or some else), that is not exists in design-time. 但是app.config将在WorkerRole1.dll.config(或其他一些)中进行转换,这在设计时是不存在的。 To cheat VS: 欺骗VS:

Unload role project .csproj. 卸载角色项目.csproj。

Find ItemGroup section with including files in project. 查找ItemGroup部分,其中包含项目中的文件。 I have next few: 我接下来几个:

<None Include="app.config">
  <SubType>Designer</SubType>
</None>
<None Include="app.Debug.config">
  <DependentUpon>app.config</DependentUpon>
</None>
<None Include="app.Release.config">
  <DependentUpon>app.config</DependentUpon>
  <SubType>Designer</SubType>
</None>

Then set before or after: 然后在之前或之后设置:

<Content Include="bin\$(Configuration)\WorkerRole1.dll.config">
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  <Link>WorkerRole1.dll.config</Link>
</Content>

This trick says to builder "WorkerRole1.dll.config" is exists as a link , this one will be obtained after build (and transformations) and include in WorkerRole package. 这个技巧说构建器“WorkerRole1.dll.config”作为链接存在,这个将在构建(和转换)之后获得并包含在WorkerRole包中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM