How to do Assembly Rebinding in T4

1k views Asked by At

I have a DLL that has a dependency on old version of System.Net.Http.Primitives.

Due to this call to this dll results in following error in a T4 Template

    Severity    Code    Description Project File    Line
    Error       Running transformation: System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
    File name: 'System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
       at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.GetDefaultHttpClientHandler()
       at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.CreatePipeline(IEnumerable`1 handlers)
       at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient..ctor(IEnumerable`1 handlers, Uri applicationUri, String installationId, String applicationKey)
       at Microsoft.WindowsAzure.MobileServices.MobileServiceClient..ctor(Uri mobileAppUri, Uri gatewayUri, String applicationKey, HttpMessageHandler[] handlers)
       at ProjectConfig.Client.Sdk.ServiceClientFactory.CreateClient(ServiceContext context)
       at Microsoft.VisualStudio.TextTemplating94605233BE6B95A7502A0E0D0103A3C2038623387426DDF6CD07E55128CF220AA59BF7AB7A33F5E6F75E73A7CECC0C89E34B7753DD4C9A424A764F106EDB782D.GeneratedTextTransformation.TransformText()

    Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Running under executable  C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe
    --- A detailed error log follows. 

    === Pre-bind state information ===
    LOG: DisplayName = System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
     (Fully-specified)
    LOG: Appbase = file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/
    LOG: Initial PrivatePath = NULL
    Calling assembly : Microsoft.WindowsAzure.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
    ===
    LOG: This bind starts in LoadFrom load context.
    WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
    LOG: Using application configuration file: C:\Users\rajivdas\AppData\Local\Microsoft\VisualStudio\14.0\devenv.exe.config
    LOG: Using host configuration file: 
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
    LOG: Post-policy reference: System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/PublicAssemblies/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/PublicAssemblies/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/PrivateAssemblies/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/PrivateAssemblies/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/CommonExtensions/Microsoft/TemplateProviders/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/CommonExtensions/Microsoft/TemplateProviders/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/CommonExtensions/Platform/Debugger/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/CommonExtensions/Platform/Debugger/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/CommonExtensions/Platform/DiagnosticsHub/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/CommonExtensions/Platform/DiagnosticsHub/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/PrivateAssemblies/DataCollectors/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/PrivateAssemblies/DataCollectors/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/PrivateAssemblies/DataCollectors/x86/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/PrivateAssemblies/DataCollectors/x86/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/PublicAssemblies/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/PublicAssemblies/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/PrivateAssemblies/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/PrivateAssemblies/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/CommonExtensions/Microsoft/TemplateProviders/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/CommonExtensions/Microsoft/TemplateProviders/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/CommonExtensions/Platform/Debugger/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/CommonExtensions/Platform/Debugger/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/CommonExtensions/Platform/DiagnosticsHub/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/CommonExtensions/Platform/DiagnosticsHub/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/PrivateAssemblies/DataCollectors/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/PrivateAssemblies/DataCollectors/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/PrivateAssemblies/DataCollectors/x86/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/PrivateAssemblies/DataCollectors/x86/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///D:/Repos/ProjectConfig/v2/Samples/Contoso/Configuration/bin/Debug/System.Net.Http.Primitives.DLL.
    WRN: Comparing the assembly name resulted in the mismatch: Major Version
    LOG: Attempting download of new URL file:///D:/Repos/ProjectConfig/v2/Samples/Contoso/Configuration/bin/Debug/System.Net.Http.Primitives/System.Net.Http.Primitives.DLL.
    LOG: Attempting download of new URL file:///D:/Repos/ProjectConfig/v2/Samples/Contoso/Configuration/bin/Debug/System.Net.Http.Primitives.EXE.
    LOG: Attempting download of new URL file:///D:/Repos/ProjectConfig/v2/Samples/Contoso/Configuration/bin/Debug/System.Net.Http.Primitives/System.Net.Http.Primitives.EXE.    Contoso.Configuration   D:\Repos\ProjectConfig\v2\Samples\Contoso\Configuration\TextTemplate1.tt    1

How can I fix this?

I tried to add to the app.config for this dll, but no effect.

    <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.29.0" newVersion="4.2.29.0" />
      </dependentAssembly>
    </assemblyBinding>
    </runtime>
3

There are 3 answers

0
Eulinky On

If you want to debug the T4:

You need to crate a configuration file "T4VSHostProcess.exe.config" in your Visual Studio path (e.g. C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE) with the appropriate assembly binding redirect.

Setting devenv.exe.config only works if not debugging.

1
MLTheKing On

The answer is in the following line:

LOG: Using application configuration file: %LOCALAPPDATA%\Microsoft\VisualStudio\14.0

E.g. C:\Users\username\AppData\Local\Microsoft\VisualStudio\14.0

If you need to redirect assembly bindings in T4, you have to add them in Visual Studio's configuration file.
Maybe it's not intuitive, but that's the way it works..

P.S. Remember to restart Visual Studio to load changes.

0
Meryan On

I solved my similar problem by having to install Microsoft HTTP Client Libraries 4.2.29.0 in the main WPF application even though I have no use for them in there. My TESTER app against my YouTubeDataV3.DLL assembly works but not when I try to consume it from WPF app (all projects were set to NET 4.5).

I did initially try to modify manually app.config and C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config as well, but I always kept seeing the same exceptions

System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Http.Primitives, Version=4.2.29.0...

System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

I think there is something wrong with NuGet in that it stores the package folder peer to the solution, instead of peer to the project's references managed by NuGet. IOW, If you have x number of clients projects applications consuming an Assembly "A" whose references are managed by NuGet, then you end up with x number of package folders scattered all over the place wherever the solution (.SLN) files are. That's really ugly plus keep in mind, all of that junk :), will get copied to bin/debug and bin/release :( one more time. What a wasteful approach.