Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I transform replace a dependentAssembly in web config?

So I'm trying to replace a dependentAssembly when someone adds my nuget package to their code.

The assembly I want to change is:

 <dependentAssembly>
    <assemblyIdentity name="Common.Logging.Core" publicKeyToken="af08829b84f0328e" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
  </dependentAssembly> 

Therefor I use this xml file and help from: Web.config transforms - the missing manual

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly xdt:Transform="Replace" xdt:Locator="Condition(./_defaultNamespace:assemblyIdentity/@name:'Common.Logging.Core')">
        <assemblyIdentity name="Common.Logging.Core" publicKeyToken="af08829b84f0328e" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="2.2.0.0" />
    </dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

But I'm getting the error: An error occurred while applying transformation to 'web.config' in project: 'blabla' has an invalid qualified name.

In noticed when I change "Replace" to "Remove" it is removing the complete dependentAssembly but somehow afterwards it adds the same dependentAssembly to the web.config again. Maybe because Common.Logging.Core dependency get's added after the web.config transform?

And maybe is this the reason that the Replace isn't working?

like image 715
Xepos Avatar asked Jun 18 '15 09:06

Xepos


People also ask

How do I create a new transformation in web config?

If you have a web application project, Right-click on web. config and choose Add Config Transform. This will add any config transforms that are missing from your project based on build configurations (i.e. if you have Production and Staging build configs, both will get a transform added).

How to do binding redirect?

To redirect one assembly version to another, use the <bindingRedirect> element. The oldVersion attribute can specify a single assembly version or a range of versions. The newVersion attribute should specify a single version. For example, <bindingRedirect oldVersion="1.1.

What does auto generate binding redirects do?

The binding redirects are added to the output configuration (app. config) file when the app is compiled. The redirects override the assembly unification that might otherwise take place.


1 Answers

Try this:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

      <!-- first completely remove the parent element -->
      <dependentAssembly xdt:Transform="RemoveAll"
                         xdt:Locator="Condition(starts-with(./_defaultNamespace:assemblyIdentity/@name,'Microsoft.Diagnostics.Tracing.EventSource'))">
      </dependentAssembly>

      <!-- then add the new block -->
      <dependentAssembly xdt:Transform="Insert">
        <assemblyIdentity name="Microsoft.Diagnostics.Tracing.EventSource" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.1.16.0" newVersion="1.1.16.0 " />
      </dependentAssembly>

    </assemblyBinding>
  </runtime>
like image 86
Emanuele Zambrano Avatar answered Sep 21 '22 06:09

Emanuele Zambrano