引用.net Core类时T4模板无法加载文件或程序集“ System.Runtime,版本= 4.2.2.0”

用.net Core 编写的T4模板类, 在T4里引用运行时,会有

错误        正在运行转换: System.IO.FileNotFoundException: 未能加载文件或程序集“System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依赖项。系统找不到指定的文件。
文件名:“System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”
   在 Microsoft.VisualStudio.TextTemplatingDF348CB3FB09E8E166E437124F9F88342823FF1D21BC7B73048E47A611D6DC38AD43D38B26E23A35527758646C26C0D989C154CDCD9B21719CC1A062236A2570.GeneratedTextTransformation.TransformText()
   在 Microsoft.VisualStudio.TextTemplating.TransformationRunner.PerformTransformation()

警告: 程序集绑定日志记录被关闭。
要启用程序集绑定失败日志记录,请将注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD)设置为 1。
注意: 会有一些与程序集绑定失败日志记录关联的性能损失。
要关闭此功能,请移除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。    Ark.Y2020.DBModel    D:\myCode\其他项目\Ark2020\Ark.Y2020.DBModel\BCBenefitConsume.tt    1    

在使用T4的模板,代码类似

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="Services.Resources.DataTransferObjects.Infrastructures" #>
<#@ import namespace="System.Collections.Generic" #> 
<#@ assembly name="$(TargetDir)Services.dll" #>
<#@ output extension=".cs" #>
public class AdminDTO
{
        <#var editableObjs = Assembly
            .GetAssembly(typeof(GenericEditable<>))
            .GetTypes()
            .Where(p => p.BaseType != null && p.BaseType.IsGenericType && p.BaseType.GetGenericTypeDefinition() == (typeof(GenericEditable<>)))
            .ToList();
        #>
}

即使项目引用System.Runtime.dll也是没有用的, 这个问题的本质是Vs2019工具在运行T4程序时的问题, 所以可以修改Vs相关的配置才可以

方法一:

参考: https://stackoverflow.com/questions/51550265/t4-template-could-not-load-file-or-assembly-system-runtime-version-4-2-0-0/54314778#54314778

C:\Users\<user>\AppData\Local\Microsoft\VisualStudio\15.0_29f8d23a\devenv.exe.config里有<configuration>-> <runtime>-> <assemblyBinding>

<dependentAssembly>
  <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
  <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="4.0.0.0"/>
</dependentAssembly>

 

方法二:

上面说了,问题本质是Vs的问题, 所以我们不用.net core编写T4的辅助类即可,使用.net Framework的项目,然后把相关的Dll复制到解决方案目录下

T4的模板如下:

<#@ template debug="Flase" hostspecific="True" language="C#" #>
<#@ assembly name="$(SolutionDir)T4dll/TC.Ab.T4.dll" #>
<#@ assembly name="$(SolutionDir)T4dll/MySql.Data.dll" #>
<#@ import namespace="TC.Ab.T4" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="System.Diagnostics" #>
<#@ output extension=".cs" #>
<#
    //Debugger.Launch(); Debugger.Break();//调试用template debug="True" hostspecific="True" language="C#"
    DbField dbRender = new DbField(this.Host.TemplateFile,"TCItravelOrder");//数据库链接名称可以不传,默认MetaDataDB
    dbRender.NamespaceStr="TC.itravel.Admin.DBModel";  
    dbRender.OnlyTable.Add("BCBenefitConsume");//只要生成的表,区分大小写
    this.WriteLine(dbRender.Render()); 
#>

 这种情况只是Vs使用了Framework版本的类, 项目本身还是core, 所以不影响项目的发布,如果是Docker发布,可以在.dockerignore文件里进行排除

posted @ 2020-03-26 08:34  吴乐章,炎刘中学  阅读(5824)  评论(2编辑  收藏  举报