如何在T4中进行程序集重新绑定

时间:2015-06-23 04:40:25

标签: visual-studio visual-studio-2013 t4

我有一个依赖于旧版System.Net.Http.Primitives的DLL。

由于此调用此dll会导致T4模板中出现以下错误

    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

我该如何解决这个问题?

我尝试添加到此dll的app.config,但没有效果。

    <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 个答案:

答案 0 :(得分:3)

答案如下:

日志:使用应用程序配置文件: %LOCALAPPDATA%\Microsoft\VisualStudio\14.0

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

如果需要在T4中重定向程序集绑定,则必须在Visual Studio的配置文件中添加它们。
也许它不直观,但这就是它的工作方式..

P.S。请记住重新启动Visual Studio 以加载更改。

答案 1 :(得分:0)

我解决了类似的问题,因为必须在主WPF应用程序中安装Microsoft HTTP Client Library 4.2.29.0,即使我在那里没有使用它们。我的TESTER应用程序对我的YouTubeDataV3.DLL程序集有效,但当我尝试从WPF应用程序中使用它时(所有项目都设置为NET 4.5)。

我最初尝试手动修改app.config和C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ config \ machine.config,但我总是看到相同的异常

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.

我认为NuGet存在一些问题,因为它将包文件夹对等存储到解决方案中,而不是与NuGet管理的项目引用相对应。 IOW,如果您有x个客户端项目应用程序正在使用程序集“A”,其引用由NuGet管理,那么最终会在解决方案(.SLN)文件所在的地方分散x个包文件夹。这真的很难看,记住,所有的垃圾:),将被复制到bin / debug和bin / release :(再一次。这是多么浪费的方法。

答案 2 :(得分:0)

如果你想调试T4:

您需要创建一个配置文件&#34; T4VSHostProcess.exe.config&#34;在Visual Studio路径中(例如C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ Common7 \ IDE),并使用适当的程序集绑定重定向。

设置devenv.exe.config仅在不调试时才有效。