T4 Templates Assembly load stopped functioning when I upgraded to .Net 4.8

0

I have a T4 template that was working when my project was using .Net 4.6.2 with the following lines in my template. I am using Visual Studio 2019.

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)MyProject\bin\x64\Debug\MyProject.FooBar.dll" #>
<#@ output extension=".cs" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Reflection" #>

After upgrading to the whole solution to .Net 4.8 I get the following error when trying to run the template.

Severity    Code    Description Project File    Line    Suppression State
Error       There was a problem loading the assembly     'C:\Repos\MyProject\bin\x64\Debug\MyProject.FooBar.dll' The following Exception was thrown:
System.BadImageFormatException: Could not load file or assembly 'file:///C:\Repos\MyProject\bin\x64\Debug\MyProject.FooBar.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
File name: 'file:///C:\Repos\MyProject\bin\x64\Debug\MyProject.FooBar.dll'
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at Microsoft.VisualStudio.TextTemplating.TransformationRunner.AttemptAssemblyLoad(String assemblyName)

=== Pre-bind state information ===
LOG: Where-ref bind. Location = C:\Repos\MyProject\bin\x64\Debug\MyProject.FooBar.dll
LOG: Appbase = file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Preview/Common7/IDE/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
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\ptaylor\AppData\Local\Microsoft\VisualStudio\16.0_9613a24e\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: Attempting download of new URL file:///C:/Repos/MyProject\bin\x64\Debug\MyProject.FooBar.dll.
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.            1   

I've looked around and found some similar problems in the past, but the fixes were use a fully qualified name (check) or try this fix T4 Template Could not load file or assembly 'System.Runtime, Version = 4.2.0.0' which I tried to no avail.

t4
.net-4.8
asked on Stack Overflow Nov 3, 2020 by PlTaylor

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0