如何修改cspkg中定义的csdef

Eva*_*van 4 msbuild powershell azure

要部署到不同的azure环境,我将修改csdef作为编译步骤的一部分来更改主机头.这样做需要为每个环境构建一次cspkg,而不是能够重用cspkg并为部署指定不同的配置.

我想在创建cspkg之后修改cspkg的csdef文件而不重新编译.那是可能的,如果是这样的话怎么样?

kni*_*hor 6

我做了类似于你所要做的事情,以区分测试环境和实时环境.首先,您需要创建一个要用于备用设置的新.csdef文件.这需要是完整的文件,因为我们只是将其与原始文件交换.现在我们需要将其添加到云项目中.右键单击云项目并选择卸载项目.再次右键单击它并选择编辑[项目名称].有一节看起来有点像这样:

<ItemGroup>
    <ServiceConfiguration Include="ServiceConfiguration.Test.cscfg" />
    <ServiceDefinition Include="ServiceDefinition.csdef" />
    <ServiceConfiguration Include="ServiceConfiguration.cscfg" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

添加一个指向新创建的文件的新ServiceDefinition项.现在找到以下行:

<Import Project="$(CloudExtensionsDir)Microsoft.WindowsAzure.targets" />
Run Code Online (Sandbox Code Playgroud)

然后添加此代码块,将TargeProfile检查编辑为您希望用于备用的构建配置,并确保它指向新的.csdef文件

<Target Name="AfterResolveServiceModel">
    <!-- This should be run after it has figured out which definition file to use
        but before it's done anything with it.  This is all a bit hard coded, but
        basically it should remove everything from the SourceServiceDefinition
        item and replace it with the one we want if this is a build for test-->
    <ItemGroup>
      <!-- This is an interesting way of saying remove everything that is in me from me-->
      <SourceServiceDefinition Remove="@(SourceServiceDefinition)" />
      <TargetServiceDefinition Remove="@(TargetServiceDefinition)" />
    </ItemGroup>
    <ItemGroup Condition="'$(TargetProfile)' == 'Test'">
      <SourceServiceDefinition Include="ServiceDefinition.Test.csdef" />
    </ItemGroup>
    <ItemGroup Condition="'$(TargetProfile)' != 'Test'">
      <SourceServiceDefinition Include="ServiceDefinition.csdef" />
    </ItemGroup>
    <ItemGroup>
      <TargetServiceDefinition Include="@(SourceServiceDefinition->'%(RecursiveDirectory)%(Filename).build%(Extension)')" />
    </ItemGroup>
    <Message Text="Source Service Definition Changed To Be: @(SourceServiceDefinition)" />
  </Target>
Run Code Online (Sandbox Code Playgroud)

要恢复正常,请右键单击项目并选择"重新加载项目".现在,在构建项目时,根据您使用的配置,它将使用不同的.csdef文件.值得注意的是,设置编辑器不知道您的第二个.csdef文件,因此如果您通过GUI添加任何新设置,则需要手动将它们添加到此备用版本.