nuget-packagexdt-transform

How to use .xdt file


I have followed the following post Nuget Config.Transform Formatting Issue to add web.config file entries in NuGet.

I have used the below xdt file in my NuGet package

    <compilation>
        <assemblies>
            <add assembly="ASSEMBLY1, Version=1.0.10, Culture=neutral" xdt:Transform="Insert"/>
            <add assembly="ASSEMBLY2, Version=1.0.11, Culture=neutral" xdt:Transform="Insert"/>
            <add assembly="ASSEMBLY3, Version=1.0.12, Culture=neutral" xdt:Transform="Insert"/>
            <add assembly="ASSEMBLY4, Version=1.0.13, Culture=neutral" xdt:Transform="Insert"/>
        </assemblies>
    </compilation>
</system.web>

and i have tried to install this NuGet am getting the following error. Error An error occurred while applying transformation to 'web.config' in project 'WebApplication60' No element in the source document matches '/configuration/system.web/compilation/assemblies/add' 0

Since the assemblies node was not found in the project. I have tried to add xdt:Transform="Insert" to assemblies node, but it causes duplicate entries in web.configfile like below

<assemblies>
                <add assembly="ASSEMBLY1, Version=1.0.10, Culture=neutral" xdt:Transform="Insert"/>
                <add assembly="ASSEMBLY2, Version=1.0.11, Culture=neutral" xdt:Transform="Insert"/>
                <add assembly="ASSEMBLY3, Version=1.0.12, Culture=neutral" xdt:Transform="Insert"/>
                <add assembly="ASSEMBLY4, Version=1.0.13, Culture=neutral" xdt:Transform="Insert"/>
<add assembly="ASSEMBLY1, Version=1.0.10, Culture=neutral" xdt:Transform="Insert"/>
                <add assembly="ASSEMBLY2, Version=1.0.11, Culture=neutral" xdt:Transform="Insert"/>
                <add assembly="ASSEMBLY3, Version=1.0.12, Culture=neutral" xdt:Transform="Insert"/>
                <add assembly="ASSEMBLY4, Version=1.0.13, Culture=neutral" xdt:Transform="Insert"/>
            </assemblies>

How to avoid this duplicate entries?


Solution

  • To avoid inserting duplicate entries, you can try to remove assemblies element first, if any, then insert a new one which has been populated with <add assembly=""/> elements afterwards :

    <compilation>
        <assemblies xdt:Transform="Remove"/>
        <assemblies xdt:Transform="Insert">
            <add assembly="ASSEMBLY1, Version=1.0.10, Culture=neutral"/>
            <add assembly="ASSEMBLY2, Version=1.0.11, Culture=neutral"/>
            <add assembly="ASSEMBLY3, Version=1.0.12, Culture=neutral"/>
            <add assembly="ASSEMBLY4, Version=1.0.13, Culture=neutral"/>
        </assemblies>
    </compilation>
    

    Or, you can try using InsertIfMissing transform to insert while an element and avoid duplicate in case it already exists :

    <compilation>
        <assemblies xdt:Transform="InsertIfMissing">
            <add assembly="ASSEMBLY1, Version=1.0.10, Culture=neutral" 
                 xdt:Transform="InsertIfMissing" xdt:Locator="Match(assembly)"/>
            <add assembly="ASSEMBLY2, Version=1.0.11, Culture=neutral"
                 xdt:Transform="InsertIfMissing" xdt:Locator="Match(assembly)"/>
            <add assembly="ASSEMBLY3, Version=1.0.12, Culture=neutral"
                 xdt:Transform="InsertIfMissing" xdt:Locator="Match(assembly)"/>
            <add assembly="ASSEMBLY4, Version=1.0.13, Culture=neutral"
                 xdt:Transform="InsertIfMissing" xdt:Locator="Match(assembly)"/>
        </assemblies>
    </compilation>