question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Intermittent System.TypeLoadExceptions and System.Security.VerificationExceptions

See original GitHub issue

Version: 7.3.1

Castle.Core version: 4.3.1.

After upgrading our solution from .NET 4.8 to .NET 6 we have seen intermittent (about one failed run in every 6-7 build server test runs) System.TypeLoadExceptions and System.Security.VerificationExceptions with the following messages:

System.Security.VerificationException example:

System.ArgumentException  GenericArguments[0], 'CompanyName.OurType[]', on 'T ExecuteLegacyQuery[T](CompanyName.IQuery`1[T])' violates the constraint of type 'T'.
---- System.Security.VerificationException  Method Castle.Proxies.ObjectProxy_35.ExecuteLegacyQuery type argument 'CompanyName.OurType[]' violates the constraint of type parameter 'T'.
   at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
   at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
   at FakeItEasy.Core.MethodInfoManager.MakeGeneric(MethodInfo methodToMakeGeneric, MethodInfo originalMethod) in CprojectsfakeiteasysrcFakeItEasyCoreMethodInfoManager.csline 112
   at FakeItEasy.Core.MethodInfoManager.c__DisplayClass5_0.GetMethodOnTypeThatImplementsInterfaceMethodb__2(f__AnonymousType4`2 methodTargetPair) in CprojectsfakeiteasysrcFakeItEasyCoreMethodInfoManager.csline 102
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at FakeItEasy.Core.MethodInfoManager.GetMethodOnTypeThatImplementsInterfaceMethod(Type type, MethodInfo method) in CprojectsfakeiteasysrcFakeItEasyCoreMethodInfoManager.csline 98
   at FakeItEasy.Core.MethodInfoManager.FindMethodOnTypeThatWillBeInvokedByMethodInfo(Type type, MethodInfo method) in CprojectsfakeiteasysrcFakeItEasyCoreMethodInfoManager.csline 46
   at FakeItEasy.Core.MethodInfoManager.c.GetMethodOnTypeThatWillBeInvokedByMethodInfob__2_0(TypeMethodInfoPair k) in CprojectsfakeiteasysrcFakeItEasyCoreMethodInfoManager.csline 41
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at FakeItEasy.Core.MethodInfoManager.GetMethodOnTypeThatWillBeInvokedByMethodInfo(Type type, MethodInfo method) in CprojectsfakeiteasysrcFakeItEasyCoreMethodInfoManager.csline 41
   at FakeItEasy.Creation.CastleDynamicProxy.CastleDynamicProxyInterceptionValidator.GetInvokedMethod(MethodInfo method, Object callTarget) in CprojectsfakeiteasysrcFakeItEasyCreationCastleDynamicProxyCastleDynamicProxyInterceptionValidator.csline 79
   at FakeItEasy.Creation.CastleDynamicProxy.CastleDynamicProxyInterceptionValidator.MethodCanBeInterceptedOnInstance(MethodInfo method, Object callTarget, String& failReason) in CprojectsfakeiteasysrcFakeItEasyCreationCastleDynamicProxyCastleDynamicProxyInterceptionValidator.csline 20
   at FakeItEasy.Creation.FakeObjectCreator.DefaultCreationStrategy.MethodCanBeInterceptedOnInstance(MethodInfo method, Object callTarget, String& failReason) in CprojectsfakeiteasysrcFakeItEasyCreationFakeObjectCreator.csline 184
   at FakeItEasy.Creation.FakeObjectCreator.MethodCanBeInterceptedOnInstance(MethodInfo method, Object callTarget, String& failReason) in CprojectsfakeiteasysrcFakeItEasyCreationFakeObjectCreator.csline 76
   at FakeItEasy.Configuration.DefaultInterceptionAsserter.AssertThatMethodCanBeInterceptedOnInstance(MethodInfo method, Object callTarget) in CprojectsfakeiteasysrcFakeItEasyConfigurationDefaultInterceptionAsserter.csline 19
   at FakeItEasy.Configuration.FakeConfigurationManager.AssertThatMemberCanBeIntercepted(ParsedCallExpression parsed) in CprojectsfakeiteasysrcFakeItEasyConfigurationFakeConfigurationManager.csline 96
   at FakeItEasy.Configuration.FakeConfigurationManager.CallTo[T](Expression`1 callSpecification) in CprojectsfakeiteasysrcFakeItEasyConfigurationFakeConfigurationManager.csline 46
   at FakeItEasy.A.CallTo[T](Expression`1 callSpecification) in CprojectsfakeiteasysrcFakeItEasyA.csline 161

System.TypeLoadException example:

System.TypeLoadException : GenericArguments[0], 'T', on 'Castle.Proxies.Invocations.ISqlDatabase_ExecuteQueryAsync_1[TEvent]' violates the constraint of type parameter 'TEvent'.
   at Castle.Proxies.ObjectProxy_63.ExecuteQueryAsync[T](String sql, Object param, CancellationToken cancellationToken, Nullable`1 commandTimeout)

The common part about all these exceptions is the error about violating the constraint of type parameter and that they are all related to mocks.

I don’t believe that this issue directly related to the FakeItEasy, but rather to the underlying library - Castle.Core.

Creating this issue for visibility.

Similar issue with moq4: https://github.com/moq/moq4/issues/1145

The issue on the Castle.Core side: https://github.com/castleproject/Core/issues/193

Issue Analytics

  • State:open
  • Created 8 months ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
blairconradcommented, Feb 2, 2023

Thanks for continuing on this, @maxcherednik. I wonder, though. The leading though is that a problem is in or below the Castle.Core project, it may be a better use of your time to follow up exclusively in https://github.com/castleproject/Core/issues/193. Then you don’t have to duplicate your efforts, and people can read all the details in one place…

(The Castle.Core code is largely over my head, and I given work and home commitments, I doubt I’m going to get enough time to educate myself and start contributing in the near future.)

1reaction
blairconradcommented, Jan 29, 2023

where does this generic parameter name coming from

No idea. FakeItEasy does not create (or change) generic parameters called “TEvent”, as far as I can tell. Nor does a quick search in GitHub reveal anything like that in the Castle.Core source.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Intermittent System.TypeLoadExceptions ...
After upgrading our solution from .NET Core 2.1 to .NET 5 we have seen intermittent (about one failed run in every 6-7 build...
Read more >
Why do my tests fail with System.Security. ...
One thing I want to point out is that apps running under the 4.0 client profile have different security behaviors than running under...
Read more >
TypeLoadException Class (System)
TypeLoadException is thrown when the common language runtime cannot find the assembly, the type within the assembly, or cannot load the type.
Read more >
VerificationException Class (System.Security)
The exception that is thrown when the security policy requires code to be type safe and the verification process is unable to verify...
Read more >
Visual Studio crashes with System.TypeLoadException
I first tried with a solution consisting of multiple projects, then reduced it to a single project. Still crash after the project is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found