Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify the csdef defined in a cspkg

To deploy to different azure environments I modify the csdef as part of the compilation step to change the host headers. Doing so requires building the cspkg once for each environment instead of being able to reuse the cspkg and specify different configs for deployment.

I would like to instead modify the csdef file of a cspkg after it has been created, without recompiling. Is that possible, and if so how?

like image 266
Evan Avatar asked Jan 16 '23 19:01

Evan


1 Answers

I've done something similar to what you're after to differentiate between test and live environments. First of all you need to create a new .csdef file that you want to use for your alternate settings. This needs to be the complete file as we're just going to swap it out with the original one. Now we need to add this to the cloud project. Right click on the cloud project and select unload project. Right click on it again and select Edit [Name of project]. There's a section that looks a bit like this:

<ItemGroup>
    <ServiceConfiguration Include="ServiceConfiguration.Test.cscfg" />
    <ServiceDefinition Include="ServiceDefinition.csdef" />
    <ServiceConfiguration Include="ServiceConfiguration.cscfg" />
</ItemGroup>

Add a new ServiceDefinition item that points to your newly created file. Now find the following line:

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

Then add this code block, editing the TargeProfile check to be the build configuration you're wanting to use for your alternate and ensuring that it points to your new .csdef file

<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>

To go back to normal, right click on the project and select Reload Project. Now when you build your project, depending on which configuration you use, it will use different .csdef files. It's worth noting that the settings editor in is not aware of your second .csdef file so if you add any new settings through the GUI you will need to add them manually to this alternate version.

like image 163
knightpfhor Avatar answered Jan 27 '23 10:01

knightpfhor