如何将 DotNetOpenAuth 程序集引用提取到 ASP.NET MVC4 解决方案的单元测试项目中? [英] How do I pull DotNetOpenAuth assembly references into unit test project for ASP.NET MVC4 solution?

查看:29
本文介绍了如何将 DotNetOpenAuth 程序集引用提取到 ASP.NET MVC4 解决方案的单元测试项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tl;dr 版本:我遇到了异常:System.IO.FileLoadException:无法加载文件或程序集DotNetOpenAuth.AspNet,版本 4.0.0.0 ...

Tl;dr version: I am stuck with the exception: System.IO.FileLoadException : Could not load file or assembly 'DotNetOpenAuth.AspNet, Version 4.0.0.0 ...

msft 在新的 MVC4 项目模板中使用了如此多的静态类和方法进行身份验证,这有点令人沮丧.想要将所有成员资格/身份验证功能包装到一个实现接口的类中,以便我可以模拟单元测试.

A bit dismayed that msft used so many static classes and methods for auth in the new MVC4 project template. Want to wrap all membership/auth functionality into a class that implements an interface so that I can mock for unit tests.

经过几个晚上的挣扎,我决定从头开始,因此我删除了所有 DotNetOpenAuth* 程序集引用和 nuget package.config 引用.我同样删除了对 Micrsoft.Aspnet.WebPages.OAuth.dll 的所有引用.

After a couple of evenings of struggling, I have decided to start from scratch, so I removed all DotNetOpenAuth* assembly references and nuget package.config references. I have similarly removed all references to Micrsoft.Aspnet.WebPages.OAuth.dll.

重新安装包:我针对解决方案中需要 dll 引用的三个项目运行 install-package dotnetopenauth.aspnet.

Reinstalling packages: I run install-package dotnetopenauth.aspnet against three projects in solution which need the dll references.

解决方案不会构建,因为我的包装器有一个包装方法:Microsoft.AspNet.WebPages.OAuth(.dll).OAuthWebSecurity.RegisteredClientData,因此我需要对这个组件的引用,所以我运行 install-针对相同的三个项目打包 microsoft.aspnet.webpages.oauth.

Solution will not build, as my wrapper has a method which wraps: Microsoft.AspNet.WebPages.OAuth(.dll).OAuthWebSecurity.RegisteredClientData and therefore I need a reference to this assy, so I run install-package microsoft.aspnet.webpages.oauth against the same three projects.

解决方案构建,但是当我运行引用 Microsoft.AspNet.WebPages.OAuth(.dll).OAuthWebSecurity.RegisteredClientData 的单元测试时,我得到一个运行时异常:System.IO.FileLoadException:无法加载文件或程序集'DotNetOpenAuth.AspNet,版本 4.0.0.0 ...

Solution builds, but when I run the unit test referencing Microsoft.AspNet.WebPages.OAuth(.dll).OAuthWebSecurity.RegisteredClientData I get a runtime exception: System.IO.FileLoadException : Could not load file or assembly 'DotNetOpenAuth.AspNet, Version 4.0.0.0 ...

所有三个项目中对此程序集的引用都引用了 4.1.0.0,我的 web.config 中的 DotNetOpenAuth.AspNet assemblyIdentity 也是如此.

The references to this assembly in all three projects reference 4.1.0.0, as does the DotNetOpenAuth.AspNet assemblyIdentity in my web.config.

我使用 resharper 来运行测试,并且测试是 nunit 风格的.

I'm using resharper to run the test, and the tests are nunit style.

最后这是我在 fuslogvw 中看到的粘贴.显然有些东西正在寻找 4.0.0.0 但我不知道该怎么做或怎么做(我有几次删除了此转储中引用的临时数据文件夹):

And finally here is a paste of what I see in fuslogvw. Obviously something is looking for 4.0.0.0 but I can't figure out what or what to do about it (I have a couple of times deleted the temp data folder referenced in this dump):

***** 组装活页夹日志条目(11/13/2012 @ 10:04:54 PM)************

***** Assembly Binder Log Entry (11/13/2012 @ 10:04:54 PM) ************

操作失败.绑定结果:hr = 0x80131040.没有可用的描述.

The operation failed. Bind result: hr = 0x80131040. No description available.

程序集管理器加载自:C:WindowsMicrosoft.NETFramework64v4.0.30319clr.dll在可执行文件 C:Program Files (x86)JetBrainsReSharperv7.0BinJetBrains.ReSharper.TaskRunner.CLR4.MSIL.exe 下运行--- 详细的错误日志如下.

Assembly manager loaded from: C:WindowsMicrosoft.NETFramework64v4.0.30319clr.dll Running under executable C:Program Files (x86)JetBrainsReSharperv7.0BinJetBrains.ReSharper.TaskRunner.CLR4.MSIL.exe --- A detailed error log follows.

=== 预绑定状态信息 ===日志:用户 = i7dave日志:DisplayName = DotNetOpenAuth.AspNet,版本=4.0.0.0,文化=中性,PublicKeyToken=2780ccd10d57b246(完全指定)日志:Appbase = file:///C:/SVN/trunk/SoAndSo45/SoAndSo.Com.Tests.Unit/bin/Debug日志:初始 PrivatePath = NULL日志:动态基数 = NULL日志:缓存库 = C:UsersdaveAppDataLocalTempkpsvgxtp.io4日志:AppName = SoAndSo.Com.Tests.Unit

=== Pre-bind state information === LOG: User = i7dave LOG: DisplayName = DotNetOpenAuth.AspNet, Version=4.0.0.0, Culture=neutral, PublicKeyToken=2780ccd10d57b246 (Fully-specified) LOG: Appbase = file:///C:/SVN/trunk/SoAndSo45/SoAndSo.Com.Tests.Unit/bin/Debug LOG: Initial PrivatePath = NULL LOG: Dynamic Base = NULL LOG: Cache Base = C:UsersdaveAppDataLocalTempkpsvgxtp.io4 LOG: AppName = SoAndSo.Com.Tests.Unit

LOG:此绑定在默认加载上下文中启动.日志:未找到应用程序配置文件.LOG:使用主机配置文件:日志:使用来自 C:WindowsMicrosoft.NETFramework64v4.0.30319configmachine.config 的机器配置文件.日志:后政策参考:DotNetOpenAuth.AspNet,版本=4.0.0.0,Culture=neutral,PublicKeyToken=2780ccd10d57b246日志:GAC 查找失败.日志:正在尝试下载新的 URL 文件:///C:/SVN/trunk/SoAndSo45/SoAndSo.Com.Tests.Unit/bin/Debug/DotNetOpenAuth.AspNet.DLL.LOG:程序集下载成功.正在尝试设置文件:C:SVN runkSoAndSo45SoAndSo.Com.Tests.UnitinDebugDotNetOpenAuth.AspNet.dllLOG:进入下载缓存设置阶段.日志:程序集名称为:DotNetOpenAuth.AspNet,版本=4.1.0.0,文化=中性,PublicKeyToken=2780ccd10d57b246警告:比较程序集名称导致不匹配:次要版本ERR: 程序集引用与找到的程序集定义不匹配.ERR:安装失败,hr = 0x80131040.ERR: 未能完成程序集的设置 (hr = 0x80131040).探测终止.

LOG: This bind starts in default load context. LOG: No application configuration file found. LOG: Using host configuration file: LOG: Using machine configuration file from C:WindowsMicrosoft.NETFramework64v4.0.30319configmachine.config. LOG: Post-policy reference: DotNetOpenAuth.AspNet, Version=4.0.0.0, Culture=neutral, PublicKeyToken=2780ccd10d57b246 LOG: GAC Lookup was unsuccessful. LOG: Attempting download of new URL file:///C:/SVN/trunk/SoAndSo45/SoAndSo.Com.Tests.Unit/bin/Debug/DotNetOpenAuth.AspNet.DLL. LOG: Assembly download was successful. Attempting setup of file: C:SVN runkSoAndSo45SoAndSo.Com.Tests.UnitinDebugDotNetOpenAuth.AspNet.dll LOG: Entering download cache setup phase. LOG: Assembly Name is: DotNetOpenAuth.AspNet, Version=4.1.0.0, Culture=neutral, PublicKeyToken=2780ccd10d57b246 WRN: Comparing the assembly name resulted in the mismatch: Minor Version ERR: The assembly reference did not match the assembly definition found. ERR: Setup failed with hr = 0x80131040. ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

谢谢!

推荐答案

我认为您应该能够将这些作为 NuGet 包安装到您的单元测试项目中,并且如果您将这些绑定重定向包含到您的测试项目的应用程序中,则它们可以正常工作.配置文件:

I think you should be able to install these as NuGet packages into your unit test project and have them work if you include these binding redirects to your test project's app.config file:

  <dependentAssembly>
    <assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
  </dependentAssembly>

如果您有一个全新的 MVC 项目并在其上运行 NuGet 的 Update-Package 命令,这些将自动创建.如果您没有运行它,并且未将 NuGet 包安装到您的单元测试项目中,那么您的单元测试项目将引用比您的 Web 项目更新的程序集.我认为这会导致您看到的负载故障.您只需要确保您的所有程序集在您的解决方案中都有通用版本,或者所需的绑定重定向已经到位.事实上,我认为绑定重定向无论如何都是必需的,因为 DotNetOpenAuth 程序集的更新时间比 Microsoft.AspNet.WebPages.OAuth 包更新的要快.

If you have a fresh MVC project and run NuGet's Update-Package command on it, these are automatically created. If you hadn't run that, and installed the NuGet packages fresh into your unit test project, then your unit test project would have references to newer assemblies than your web project did. That would cause the load failure you saw, I think. You just have to make sure that either all your assemblies have common versions across your solution, or that the required binding redirects are in place. And in fact I think the binding redirects are required anyway, since the DotNetOpenAuth assemblies have been updated more recently than the Microsoft.AspNet.WebPages.OAuth package has.

这篇关于如何将 DotNetOpenAuth 程序集引用提取到 ASP.NET MVC4 解决方案的单元测试项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆