Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

MissingMethodException when calling extension method
JimCounts
#1 Posted : Wednesday, May 16, 2012 4:32:13 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/16/2012(UTC)
Posts: 7
Location: California

Thanks: 1 times
Was thanked: 2 time(s) in 2 post(s)
I have an MVC 3 demo site which I wrote to accompany a series of blog posts located here: bitbucket

The project uses an extension method from ApprovalTests and the extension method works fine under ordinary debugging, under the Visual Studio test runner and under the DevExpress test runner.

But when the test runs under NCrunch, it fails with a MissingMethodException:

Quote:
System.MissingMethodException: Method not found: 'System.Web.Mvc.ViewResult ApprovalUtilities.Asp.Mvc.MvcUtilites.Explicit(System.Web.Mvc.ViewResult)'.
at MVCTestSite.Controllers.HomeController.Index(IEnumerable`1 cars)
at MVCTestSite.Controllers.HomeController.Index() in ...\HomeController.cs:line 26#0
at MVCTestSite.Tests.Controllers.HomeControllerTest.Index() in...\HomeControllerTest.cs:line 24#1


I can work around the problem with the NCRUNCH constant:

HomeController.cs:
Code:

        private ActionResult Index(IEnumerable<ICar> cars)
        {
#if NCRUNCH
            return View("Index", cars);
#else
            return View(cars).Explicit();
#endif
        }



Reversing the condition produces the missing method exception. I've tried a variety of configuration options and the exception is petty consistent. Just a few things I can remember trying:

  1. Instrumentation on/off
    Include ..\**.*
    Use ordinary static method syntax instead of extension syntax.
    Debug vs Release
    Any CPU vs x86
    MSTest vs xUnit
    v1.38b vs v1.39b


Maybe I'm missing something obvious, or maybe not. Hopefully the example code can help track down the problem.

Thanks,
Jim
Remco
#2 Posted : Wednesday, May 16, 2012 11:11:18 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 4,198

Thanks: 553 times
Was thanked: 611 time(s) in 591 post(s)
Hi Jim,

Thanks for posting! I've just tried running your source code myself and it seemed to work without problems for me, so I suspect this is caused by something in your environment.

Does the problem occur consistently for you? Or does a reset of the NCrunch engine rectify it?

Make sure to check the contents of the GAC to be sure that the version of approval tests you are referencing with your project is not being confused with another version of the same assembly already installed on your machine. At runtime, the CLR will always prefer to reference GAC assemblies where strong names are available - even though NCrunch may have built your project against a different assembly.
JimCounts
#3 Posted : Thursday, May 17, 2012 12:44:57 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/16/2012(UTC)
Posts: 7
Location: California

Thanks: 1 times
Was thanked: 2 time(s) in 2 post(s)
Hi,

The problem is consistent, resetting the engine doesn't fix it. I also checked the GAC as you suggested but there aren't any copes of ApprovalUtilites in there.

I've got two different machines that the test is failing on. I set up both machines so it's possible I introduced the same variable into both environments. If you have any more ideas let me know, in the meantime I'll see if I can reproduce the error in a clean VM or something.

--Jim
Remco
#4 Posted : Thursday, May 17, 2012 4:06:38 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 4,198

Thanks: 553 times
Was thanked: 611 time(s) in 591 post(s)
I think what would be interesting is to try and find the assembly being resolved by NCrunch's test environment and find out why it's different to the one you've referenced.

Try breaking into the code using NCrunch's debugging options and have a look at the list of modules loaded using your debugger. NCrunch will always pre-load the assemblies it believes are required in the test domain, so the DLL should already exist in the list. Which DLL was loaded? Is it the one from the NCrunch workspace? Does it have any structural differences to the ApprovalTests DLL referenced from your project in the solution?
JimCounts
#5 Posted : Thursday, May 17, 2012 1:49:16 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/16/2012(UTC)
Posts: 7
Location: California

Thanks: 1 times
Was thanked: 2 time(s) in 2 post(s)
Hello again,

I followed your suggestion and I think I've isolated the problem. After breaking into the code and looking at the loaded modules, everything seemed fine. The correct version of ApprovalUtilities was loaded. Poking around in the debugger, I tried executing the bad line of code in the quick watch window, and later in the immediate window. In the immediate window I got this:

Quote:
ApprovalUtilities.Asp.Mvc.MvcUtilites.Explicit(View(repository.GetCars()))
The best overloaded method match for 'ApprovalUtilities.Asp.Mvc.MvcUtilites.Explicit(System.Web.Mvc.ViewResult)' has some invalid arguments
ApprovalUtilities.Asp.Mvc.MvcUtilites.Explicit(new ViewResult())
The type or namespace name 'ViewResult' is not valid in this scope


So I went back to the module list and found this:

Quote:

System.Web.Mvc.dll C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll
System.Web.Mvc.dll C:\Windows\assembly\GAC_MSIL\System.Web.Mvc\2.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll


Since two versions of MVC are loaded, the runtime appears to be having trouble deciding what a ViewResult is, and can't decide whether it can use the extension method in ApprovalUtilities. I debugged the same test under the VS test runner and sure enough, only MVC v3 was loaded. I uninstalled MVC v2 from one of my machines, and sure enough, the test works in NCrunch now. You could probably reproduce on your side by grabbing MVC v2.

The project is MVC 3, but ApprovalUtilities is built against MVC 2, so this is probably why NCrunch decided to load it. I still have v2 on my machine because I still have some sites that are built against v2. If you think I'm right that the two versions of MVC are causing the problem, then I'm happy at this point to know what the problem was. But I'd also like to put MVC 2 back on the machine at some point in the future.

Any thoughts?
Remco
#6 Posted : Thursday, May 17, 2012 10:10:26 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 4,198

Thanks: 553 times
Was thanked: 611 time(s) in 591 post(s)
Hi Jim,

You've hit the nail on the head with the duplicate assemblies. The CLR tends to get confused if two assemblies with the same name are loaded into the App Domain, and in this situation it can be tough to know what is the correct answer - as the runtime code seems to depend upon both versions of the file at the same time.

NCrunch is supposed to statically bind its test runtime to the same DLLs that were used to build a project, so an interesting piece of information for me would be to establish which version of System.Web.Mvc was used during the build of your project. I have some changes in progress that may solve this problem in a future version, but until we can find out which DLL was used in the build, I can't be sure they'll work.

I can have a go at reproducing this situation locally. I'm wondering if there's any chance you might be interested in loading MVC2 back into your GAC and having a look for me which assembly was resolved in the build, as this would reduce the risk that I'm unable to accurately reproduce your build environment. The easiest way to find out which assemblies NCrunch is building against is through the following steps:

1. Open the solution, let it load the projects and build them all
2. Right click on the built project in the Tests Window (you may need to adjust the filter to do this)
3. Choose Advanced->Browse to workspace
4. Find the .proj file in the workspace that NCrunch has constructed for the build
5. Open the .proj file and examine the <AssemblyReference> tags to see which assemblies are being referenced.

Thanks!

Remco
JimCounts
#7 Posted : Friday, May 18, 2012 3:39:18 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/16/2012(UTC)
Posts: 7
Location: California

Thanks: 1 times
Was thanked: 2 time(s) in 2 post(s)
No AssemblyReferences per-se. Perhaps because this is an MVC proj, they all look like file references to me.

Quote:
<ItemGroup>
<Reference Include="c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll" />
<Reference Include="c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.Helpers.dll" />
<Reference Include="c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.WebPages.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Microsoft.CSharp.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.ComponentModel.DataAnnotations.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Configuration.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Core.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.DataSetExtensions.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.Entity.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.DirectoryServices.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Drawing.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.EnterpriseServices.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Web.Abstractions.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Web.ApplicationServices.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Web.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Web.DynamicData.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Web.Entity.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Web.Extensions.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Web.Routing.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Web.Services.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Xml.dll" />
<Reference Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Xml.Linq.dll" />
<Reference Include="J:\7080\3\_ncrunchreferences\ApprovalTests.dll" />
<Reference Include="J:\7080\3\_ncrunchreferences\ApprovalUtilities.dll" />
<Reference Include="J:\7080\3\_ncrunchreferences\EntityFramework.dll" />
</ItemGroup>
Remco
#8 Posted : Friday, May 18, 2012 6:25:49 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 4,198

Thanks: 553 times
Was thanked: 611 time(s) in 591 post(s)
Thanks, this helps a lot. I'll see what I can do about arranging a proper fix for this.

Cheers,

Remco
1 user thanked Remco for this useful post.
JimCounts on 5/18/2012(UTC)
JimCounts
#9 Posted : Friday, May 18, 2012 1:43:21 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/16/2012(UTC)
Posts: 7
Location: California

Thanks: 1 times
Was thanked: 2 time(s) in 2 post(s)
Thanks Remco.

If you are interested, here's the post about this test site. I feature NCrunch throughout the article.

ApprovalTests and MVC Views: Getting Started

--Jim
Remco
#10 Posted : Saturday, May 19, 2012 12:00:35 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 4,198

Thanks: 553 times
Was thanked: 611 time(s) in 591 post(s)
Nice blog post and thanks for the mention!
Remco
#11 Posted : Wednesday, May 23, 2012 2:54:33 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 4,198

Thanks: 553 times
Was thanked: 611 time(s) in 591 post(s)
HI Jim -

I've had another go at reproducing this with both versions of MVC installed, though it all seems to run properly for me.

I still think there must be something else in your environment that is causing a difference in behaviour. I'd really like to have a look at a bug report from your NCrunch after you've loaded the project and finished a full build/test run. I realise I'm asking you to install MVC2 all over again, so please only do this if you can make time for it.


Cheers,

Remco
JimCounts
#12 Posted : Wednesday, May 23, 2012 2:23:14 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/16/2012(UTC)
Posts: 7
Location: California

Thanks: 1 times
Was thanked: 2 time(s) in 2 post(s)
Not a problem.

In a strange twist of fate the hard drive died on the machine where I removed MVC 2. Restore from backup and *poof* instant MVC 2 reinstall.

Just submitted the bug report now.
1 user thanked JimCounts for this useful post.
Remco on 5/23/2012(UTC)
JimCounts
#13 Posted : Tuesday, June 19, 2012 3:19:07 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/16/2012(UTC)
Posts: 7
Location: California

Thanks: 1 times
Was thanked: 2 time(s) in 2 post(s)
Tested with 1.40b. Fixed!

Thanks, Remco. Very cool release.
1 user thanked JimCounts for this useful post.
Remco on 6/19/2012(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.090 seconds.