无法使用moles来模拟MVC框架

发布于 2024-11-19 04:37:29 字数 1284 浏览 3 评论 0原文

我正在尝试为 System.Web.Mvc DLL 版本 3 生成摩尔,但出现以下错误:

 Moles : info : metadata : loading C:\TFS.as12.Projects\Project X\Main\Source\3rdPartyComponents\MVC3\System.Web.Mvc.dll
    Moles : info : compilation : output assembly name: System.Web.Mvc.Moles
Moles : warning : metadata : failed to load module System.Web.Mvc.dll:
Moles : warning : metadata : Inheritance security rules violated by type: 'System.Web.Mvc.CompareAttribute'. Derived types must either match the security accessibility of the base type or be less accessible.
Moles : warning : metadata : Inheritance security rules violated by type: 'System.Web.Mvc.RemoteAttribute'. Derived types must either match the security accessibility of the base type or be less accessible.
Moles : error : code : assembly contains no types or failed to load properly
  00:00:00.53> moles generator 1 errors, 3 warnings

    Moles compilation FAILED - 6,18794176354816s
C:\Program Files (x86)\Microsoft Moles\bin\Microsoft.Moles.targets(79,5): error MSB3073: The command ""C:\Program Files (x86)\Microsoft Moles\bin\moles.exe" @"C:\TFS.as12.Projects\Project X\Main\Source\X.Web\X.Web.Base.Mvc.UnitTest\obj\Debug\Moles\moles.args"" exited with code -1002.

我在 Microsoft 论坛上看到了更多有关此问题的问题,但从未得到答案。 有人有解决办法吗?

I am trying to generate moles for the System.Web.Mvc DLL version 3, but I get the following error:

 Moles : info : metadata : loading C:\TFS.as12.Projects\Project X\Main\Source\3rdPartyComponents\MVC3\System.Web.Mvc.dll
    Moles : info : compilation : output assembly name: System.Web.Mvc.Moles
Moles : warning : metadata : failed to load module System.Web.Mvc.dll:
Moles : warning : metadata : Inheritance security rules violated by type: 'System.Web.Mvc.CompareAttribute'. Derived types must either match the security accessibility of the base type or be less accessible.
Moles : warning : metadata : Inheritance security rules violated by type: 'System.Web.Mvc.RemoteAttribute'. Derived types must either match the security accessibility of the base type or be less accessible.
Moles : error : code : assembly contains no types or failed to load properly
  00:00:00.53> moles generator 1 errors, 3 warnings

    Moles compilation FAILED - 6,18794176354816s
C:\Program Files (x86)\Microsoft Moles\bin\Microsoft.Moles.targets(79,5): error MSB3073: The command ""C:\Program Files (x86)\Microsoft Moles\bin\moles.exe" @"C:\TFS.as12.Projects\Project X\Main\Source\X.Web\X.Web.Base.Mvc.UnitTest\obj\Debug\Moles\moles.args"" exited with code -1002.

I have seen more questions about this on the Microsoft forums, but never an answer.
Does anyone have a solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何止钟意 2024-11-26 04:37:29

这与已知问题相关。如果您决定将 Moles 与 MVC3 一起使用,您可以执行以下操作(我已经这样做了):

  1. 下载 MVC3 源代码
  2. 从 Properties/AssemblyInfo.cs 中删除 [SecurityTransparent]
  3. 使用 sn.exe 从真正的 MVC dll 中提取 MS 公钥:“sn -e System.Web.Mvc.dll ms_public_key.snk”
  4. 告诉 VS 在签名时使用该公钥伪造的 MVC dll(您可以在项目属性、签名下执行此操作)。确保选中“仅延迟标志”框。
  5. 建造。现在你有了一个用 MS 公钥签名的假 MVC dll。但您不能将其用于任何用途,因为它无法通过签名验证。
  6. 再次使用 sn.exe 为您的假 dll 注册跳过验证:“sn -Vr System.Web.Mvc.dll” <--- 这需要是您的一个
  7. GAC 您的假一个带有 gacutil.exe 的:“gacutil -if System.Web.Mvc.dll”<--- 又是假的一个
  8. 运行鼹鼠。我强烈建议您对整个 dll 进行存根/摩尔操作,因为您将不想再次执行此操作。
  9. 删除跳过验证:“sn -Vu System.Web.Mvc.dll”<---假的
  10. 恢复真实的dll:“gacutil -if System.Web.Mvc.dll”<---< em>真实
  11. 删除你邪恶的假MVC dll,以免有人不小心使用它。

由于您所做的公钥切换,您在步骤 8 中生成的 System.Web.Mvc.Moles.dll 将引用真正的 MVC dll。您可以根据自己的喜好使用它来存根/摩尔 MVC 类。

This is related to a known issue. If you have your heart set on using Moles with MVC3, you can do the following (I have done this):

  1. Download the MVC3 source code.
  2. Remove [SecurityTransparent] from Properties/AssemblyInfo.cs
  3. Extract the MS public key from the real MVC dll using sn.exe: "sn -e System.Web.Mvc.dll ms_public_key.snk"
  4. Tell VS to use that public key when signing your fake MVC dll (you can do this under project properties, signing). Make sure you check the "delay sign only" box.
  5. Build. Now you have a fake MVC dll that is signed with the MS public key. But you can't use it for anything because it won't pass muster with signing verification.
  6. Use sn.exe again to register a skip-verification for your fake dll: "sn -Vr System.Web.Mvc.dll" <--- this needs to be your fake one
  7. GAC your fake one with gacutil.exe: "gacutil -if System.Web.Mvc.dll" <--- again, the fake one
  8. Run moles. I strongly recommend you stub/mole the entire dll because you're not going to want to have to do this again.
  9. Remove the skip-verification: "sn -Vu System.Web.Mvc.dll" <--- the fake one
  10. Restore the real dll: "gacutil -if System.Web.Mvc.dll" <--- the real one
  11. Delete your evil fake MVC dll lest someone accidently use it.

The System.Web.Mvc.Moles.dll you generated in step 8 will reference the real MVC dll, thanks to the public key switching you did. You can use it to stub/mole MVC classes to your heart's content.

蒗幽 2024-11-26 04:37:29

您可以尝试排除有问题的类型:

<Moles ...
   <StubGeneration ...
       <Types>
          <Remove TypeName="System.Web.Mvc.CompareAttribute" />
          <Remove TypeName="System.Web.Mvc.RemoteAttribute" />
       </Types>
   <MoleGeneration>
       <Types>
          <Remove TypeName="System.Web.Mvc.CompareAttribute" />
          <Remove TypeName="System.Web.Mvc.RemoteAttribute" />
       </Types>
   </MoleGeneration>

You could try excluding problematic types:

<Moles ...
   <StubGeneration ...
       <Types>
          <Remove TypeName="System.Web.Mvc.CompareAttribute" />
          <Remove TypeName="System.Web.Mvc.RemoteAttribute" />
       </Types>
   <MoleGeneration>
       <Types>
          <Remove TypeName="System.Web.Mvc.CompareAttribute" />
          <Remove TypeName="System.Web.Mvc.RemoteAttribute" />
       </Types>
   </MoleGeneration>
毁梦 2024-11-26 04:37:29

要添加 @bhamlin 提到的内容,在步骤 #2 中,我还必须在 AssemblyInfo.cs 中进行以下更改以生成摩尔:

修改

[assembly: AllowPartiallyTrustedCallers]

[assembly: AllowPartiallyTrustedCallers(PartialTrustVisibilityLevel = PartialTrustVisibilityLevel.NotVisibleByDefault)]

,因为这是 System.ComponentModel.DataAnnotations.dll 中定义的内容

<块引用>

派生类型必须与基类型的安全可访问性相匹配,或者难以访问

此程序集中的 CompareAttribute 基类(即 ValidationAttribute)。

To add to what @bhamlin mentioned, in step #2, I also had to make the following changes in AssemblyInfo.cs to generate moles:

Modify

[assembly: AllowPartiallyTrustedCallers]

to

[assembly: AllowPartiallyTrustedCallers(PartialTrustVisibilityLevel = PartialTrustVisibilityLevel.NotVisibleByDefault)]

as this is what's defined in System.ComponentModel.DataAnnotations.dll

Derived types must either match the security accessibility of the base type or be less accessible

Base class for CompareAttribute, which is ValidationAttribute, is in this assembly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文