Ninject.MockingKernel.Moq throws exception (some version conflict with Moq)

935 views
Skip to first unread message

Julian

unread,
Sep 23, 2011, 11:32:12 PM9/23/11
to nin...@googlegroups.com

When I run the following unit test (xunit) via Resharper, I get an exception (see below).

Maybe someone can push a new version, which depends on the current Moq version to the Nuget library.
A quick question: How can I enforce a specific version of a library when using Nuget.
Or more general: Is there nice way which one can use to shield himself from such problems?

namespace ClassLibrary1

{

  public class Class1

  {


    [Fact]

    public void Bla()

    {

      var kernel = new MoqMockingKernel();

      var mock = kernel.Get<IFoo>();

      int x = mock.Bar();

      Assert.Equal(0, x);

    }

  }


  public interface IFoo

  {

    int Bar();

  }

}


The stacktrace:

Bla : Failed

System.IO.FileLoadException: Could not load file or assembly 'Moq, Version=4.0.812.4, Culture=neutral, PublicKeyToken=69f491c39445e920' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
at Ninject.MockingKernel.Moq.MoqMockProvider.Create(IContext context)
at Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Context.cs: line 157
at Ninject.KernelBase.<Resolve>b__7(IContext context) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\KernelBase.cs: line 375
at System.Linq.Enumerable.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
at System.Linq.Enumerable.Single(IEnumerable`1 source)
at Ninject.ResolutionExtensions.Get(IResolutionRoot rootIParameter[] parameters) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Syntax\ResolutionExtensions.cs: line 37
at ClassLibrary1.Class1.Bla() in Class1.cs: line 19

Julian

unread,
Sep 24, 2011, 10:28:42 AM9/24/11
to nin...@googlegroups.com
I was able to resolve this error by adding an app.config file with an assemblyBinding.

More information:

Julian

unread,
Sep 24, 2011, 12:08:36 PM9/24/11
to nin...@googlegroups.com
Ok, now I am fighting the next exception.
When using the same example (see above) I get a TypeAccessException.

Bla : Failed

System.TypeAccessException: Attempt by security transparent method 'Ninject.MockingKernel.Moq.MoqMockProvider.Create(Ninject.Activation.IContext)' to access security critical type 'Moq.Mock' failed.

Assembly 'Ninject.MockingKernel.Moq, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7' is marked with the AllowPartiallyTrustedCallersAttribute, and uses the level 2 security transparency model.  Level 2 transparency causes all methods in AllowPartiallyTrustedCallers assemblies to become security transparent by default, which may be the cause of this exception.
at Ninject.MockingKernel.Moq.MoqMockProvider.Create(IContext context) in c:\Projects\Ninject\Maintenance2.2\ninject.mockingkernel\src\Ninject.MockingKernel.Moq\MoqMockProvider.cs: line 64

at Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Context.cs: line 157
at Ninject.KernelBase.<Resolve>b__7(IContext context) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\KernelBase.cs: line 375
at System.Linq.Enumerable.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
at System.Linq.Enumerable.Single(IEnumerable`1 source)
at Ninject.ResolutionExtensions.Get(IResolutionRoot rootIParameter[] parameters) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Syntax\ResolutionExtensions.cs: line 37
at ClassLibrary1.Class1.Bla() in Class1.cs: line 20


I read [1] and [2] but I am still not sure if it is my fault or Ninject.MockingKernel.Moq.
MoqMockProvider.Create is marked with [System.Security.SecuritySafeCritical] which should do the trick.

This guy seems to have the same problem.

Julian

unread,
Sep 25, 2011, 8:41:22 AM9/25/11
to nin...@googlegroups.com
Ok, I tried to clone Ninject.MockingKernel, build it and use the resulting assemblies.
It seems to work now. - Could someone push a new version to the NuGet gallary?

Cheers, Julian


Julian

unread,
Sep 27, 2011, 9:58:22 AM9/27/11
to nin...@googlegroups.com
I created an issue for this: https://github.com/ninject/ninject.mockingkernel/issues/7
Is there any chance for this happening in the near future?

Cheers, Julian
Reply all
Reply to author
Forward
0 new messages