Hi Support,
I have just upgraded to your latest version and lots of my test are failing.
It only fails when the item in the generic list is an interface e.g. List<IEvent>, please note this works correctly in version 3.17.0.2
Error
Code line: return Expression.Lambda<Func<object, object>>(boxedResult, parameter).Compile();
Exception:
OneTimeSetUp: System.Exception : Error registering message type List`1 with TradeMessageSerialiser.
----> System.Exception : Error creating WeakGetter for property Item on type System.Collections.Generic.List`1[Test.Events.IEvent].
----> System.InvalidProgramException : Common Language Runtime detected an invalid program.
Visual Studio 2017 Professional 15.8.2
Windows 10 Professional 64 bit
I can't upgrade until this issue is resolved.
Build/Test Issues
Test failing on v3.18.0.2 but was not failing in v.3.17.0.2
Started by Dinesh on 8,784 views
Remco NCrunch Developer
#12611
04 Sep 2018 01:05 UTC
Hi, thanks for sharing this issue.
I can't think of anything in the v3.18 release that would have affected this kind of functionality. Are you sure this wasn't introduced due to another change in your toolset? (i.e. a VS upgrade).
Does setting NCrunch in compatibility mode make any difference?
Could you share the full code needed to reproduce this issue?
Can you confirm that this problem appears only when running the test under NCrunch and not with any other runner?
I can't think of anything in the v3.18 release that would have affected this kind of functionality. Are you sure this wasn't introduced due to another change in your toolset? (i.e. a VS upgrade).
Does setting NCrunch in compatibility mode make any difference?
Could you share the full code needed to reproduce this issue?
Can you confirm that this problem appears only when running the test under NCrunch and not with any other runner?
Edited 04 Sep 2018 01:40 UTC
100% it is the nCrunch v3.18 release, I downgraded to v3.17 and it works, I then upgraded back v3.18 and it failed, then downgraded one more time to v3.17 and it works.
Remco NCrunch Developer
#12616
04 Sep 2018 07:33 UTC
Dinesh wrote:100% it is the nCrunch v3.18 release, I downgraded to v3.17 and it works, I then upgraded back v3.18 and it failed, then downgraded one more time to v3.17 and it works.
Does setting NCrunch in compatibility mode make any difference?
Could you share the full code needed to reproduce this issue?
Can you confirm that this problem appears only when running the test under NCrunch and not with any other runner?
private class TestMessage2
{
public DateTime DateTimeData { get; set; }
public string StringData { get; set; }
public int IntData { get; set; }
}
[Test]
public void TestNotWorking()
{
RegisterMessageType<List<TestMessage2>>();
}
private void RegisterMessageType<T>()
{
var messageType = typeof(T);
try
{
Build(messageType);
return;
}
catch (Exception ex)
{
throw new Exception($"Error registering message type {messageType.Name} with type.", ex);
}
}
private void Build(Type classType)
{
if (classType == null) throw new ArgumentNullException(nameof(classType));
// It is important that the schema stores the properties in a predictable order:
// a) as xml comparison tools used in unit tests are sensitive to field order.
// b) it makes log inspection etc much easier.
// For that reason they are sorted alphabetically regardless of definition order on the classes.
var propertyInfos = classType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (var p in propertyInfos)
{
var weakGetter = CreateWeakGetter(p);
}
return;
}
/// <summary>
/// Create a weakly typed getter, i.e. one that takes an instance of type object as the argument and returns the boxed
/// result as an object.
/// </summary>
/// <param name="propertyInfo">PropertyInfo of the property to create a getter for.</param>
/// <returns>A compiled weakly typed property getter delegate.</returns>
private Func<object, object> CreateWeakGetter(PropertyInfo propertyInfo)
{
try
{
// We want to handle the class instance as an object so that we can work with a consistent delegate type across
// all mapping.
var parameter = Expression.Parameter(typeof(object));
// Convert the class instance argument from object into the declaring type.
// ReSharper disable once AssignNullToNotNullAttribute
var converted = Expression.Convert(parameter, propertyInfo.DeclaringType);
// Invoke the getter on the typed instance.
var property = Expression.Property(converted, propertyInfo);
// Box the result (convert to object).
var boxedResult = Expression.Convert(property, typeof(object));
// Build the delegate.
return Expression.Lambda<Func<object, object>>(boxedResult, parameter).Compile();
}
catch (Exception ex)
{
throw new Exception($"Error creating WeakGetter for property {propertyInfo.Name} on type {propertyInfo.ReflectedType}. ", ex);
}
}
Remco NCrunch Developer
#12622
04 Sep 2018 23:29 UTC
Thanks for sharing this code! The code does produce an exception for me, but I don't think it's the one giving you trouble. I receive the following:
System.Exception: Error registering message type List`1 with type. ---> System.Exception: Error creating WeakGetter for property Item on type System.Collections.Generic.List`1[UnitTestProject8.UnitTest1+TestMessage2]. ---> System.ArgumentException: Incorrect number of arguments supplied for call to method 'TestMessage2 get_Item(Int32)'
Parameter name: property
I get the same result in both NCrunch and VSTest, so I don't think this is the runner problem that you've encountered. Can you confirm?
System.Exception: Error registering message type List`1 with type. ---> System.Exception: Error creating WeakGetter for property Item on type System.Collections.Generic.List`1[UnitTestProject8.UnitTest1+TestMessage2]. ---> System.ArgumentException: Incorrect number of arguments supplied for call to method 'TestMessage2 get_Item(Int32)'
Parameter name: property
I get the same result in both NCrunch and VSTest, so I don't think this is the runner problem that you've encountered. Can you confirm?
Have you run the code in v17, you should not get any error.
Test works in Resharper test runner and MSTest using the NUnit Test Adapter, just does not work with NCrunch. 100% NCrunch issue.
Here is exception from NCrunch
System.Exception : Error registering message type List`1 with type.
----> System.Exception : Error creating WeakGetter for property Item on type System.Collections.Generic.List`1[TestName.Program+TestMessage2].
----> System.InvalidProgramException : Common Language Runtime detected an invalid program.
at TestName.Program.RegisterMessageType[T]() in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 46
at TestName.Program.TestNotWorking() in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 32
--Exception
at TestName.Program.CreateWeakGetter(PropertyInfo propertyInfo) in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 97
at TestName.Program.Build(Type classType) in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 62
at TestName.Program.RegisterMessageType[T]() in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 41
--InvalidProgramException
at System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method)
at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target)
at System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate()
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
at System.Linq.Expressions.Expression`1.Compile()
at TestName.Program.CreateWeakGetter(PropertyInfo propertyInfo) in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 93
here is code
Test works in Resharper test runner and MSTest using the NUnit Test Adapter, just does not work with NCrunch. 100% NCrunch issue.
Here is exception from NCrunch
System.Exception : Error registering message type List`1 with type.
----> System.Exception : Error creating WeakGetter for property Item on type System.Collections.Generic.List`1[TestName.Program+TestMessage2].
----> System.InvalidProgramException : Common Language Runtime detected an invalid program.
at TestName.Program.RegisterMessageType[T]() in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 46
at TestName.Program.TestNotWorking() in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 32
--Exception
at TestName.Program.CreateWeakGetter(PropertyInfo propertyInfo) in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 97
at TestName.Program.Build(Type classType) in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 62
at TestName.Program.RegisterMessageType[T]() in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 41
--InvalidProgramException
at System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method)
at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target)
at System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate()
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
at System.Linq.Expressions.Expression`1.Compile()
at TestName.Program.CreateWeakGetter(PropertyInfo propertyInfo) in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 93
here is code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;
using System.Net.Security;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using Nerdle.AutoConfig;
using NUnit.Framework;
namespace TestName
{
[TestFixture]
class Program
{
static void Main(string[] args)
{
}
private class TestMessage2
{
public DateTime DateTimeData { get; set; }
public string StringData { get; set; }
public int IntData { get; set; }
}
[Test]
public void TestNotWorking()
{
RegisterMessageType<List<TestMessage2>>();
}
private void RegisterMessageType<T>()
{
var messageType = typeof(T);
try
{
Build(messageType);
return;
}
catch (Exception ex)
{
throw new Exception($"Error registering message type {messageType.Name} with type.", ex);
}
}
private void Build(Type classType)
{
if (classType == null) throw new ArgumentNullException(nameof(classType));
// It is important that the schema stores the properties in a predictable order:
// a) as xml comparison tools used in unit tests are sensitive to field order.
// b) it makes log inspection etc much easier.
// For that reason they are sorted alphabetically regardless of definition order on the classes.
var propertyInfos = classType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (var p in propertyInfos)
{
var weakGetter = CreateWeakGetter(p);
}
return;
}
/// <summary>
/// Create a weakly typed getter, i.e. one that takes an instance of type object as the argument and returns the boxed
/// result as an object.
/// </summary>
/// <param name="propertyInfo">PropertyInfo of the property to create a getter for.</param>
/// <returns>A compiled weakly typed property getter delegate.</returns>
private Func<object, object> CreateWeakGetter(PropertyInfo propertyInfo)
{
try
{
// We want to handle the class instance as an object so that we can work with a consistent delegate type across
// all mapping.
var parameter = Expression.Parameter(typeof(object));
// Convert the class instance argument from object into the declaring type.
// ReSharper disable once AssignNullToNotNullAttribute
var converted = Expression.Convert(parameter, propertyInfo.DeclaringType);
// Invoke the getter on the typed instance.
var property = Expression.Property(converted, propertyInfo);
// Box the result (convert to object).
var boxedResult = Expression.Convert(property, typeof(object));
// Build the delegate.
return Expression.Lambda<Func<object, object>>(boxedResult, parameter).Compile();
}
catch (Exception ex)
{
throw new Exception(
$"Error creating WeakGetter for property {propertyInfo.Name} on type {propertyInfo.ReflectedType}. ",
ex);
}
}
}
}
Edited 05 Sep 2018 07:32 UTC
Remco NCrunch Developer
#12625
05 Sep 2018 08:00 UTC
Can you confirm if you are using a 3rd party profiler such as TypeMock or JustMock? And if so, does turning this off make any difference?
I'm not using them here is my using statement
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using NUnit.Framework;
You should be able to see it the code I published working with v3.17 and NOT working with v3.18.
Also if you change line number 26
from RegisterMessageType<List<TestMessage2>>();
to RegisterMessageType<TestMessage2>();
then it will work, it is to do with generics.
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using NUnit.Framework;
You should be able to see it the code I published working with v3.17 and NOT working with v3.18.
Also if you change line number 26
from RegisterMessageType<List<TestMessage2>>();
to RegisterMessageType<TestMessage2>();
then it will work, it is to do with generics.
Edited 05 Sep 2018 08:33 UTC
Remco NCrunch Developer
#12628
05 Sep 2018 08:36 UTC
Thanks, I've managed to produce the result you're seeing now. For some reason, when I created a test project, it defaulted to .NET Core 2.1... This gave me a very different result. I'm digging deep into the problem now and will let you know as soon as I have more information.
That is good news you can replicate the problem, hopefully not too long before you resolve it.
Remco NCrunch Developer
#12631
05 Sep 2018 09:27 UTC
Got it! This one really had me perplexed ...
The problem is caused by a regression in v3.18. NCrunch is now incorrectly turning on the MSFakes/Intellitrace profiler when running tests. Most of the time this is harmless, but it looks like the code you've described has a compatibility issue with Intellitrace.
I've done some experimentation with settings, but it doesn't look like there's any way to work around this with configuration. So we'll fix it with a new version. v3.19 is due for release anyway. I should have a build up for you soon.
The problem is caused by a regression in v3.18. NCrunch is now incorrectly turning on the MSFakes/Intellitrace profiler when running tests. Most of the time this is harmless, but it looks like the code you've described has a compatibility issue with Intellitrace.
I've done some experimentation with settings, but it doesn't look like there's any way to work around this with configuration. So we'll fix it with a new version. v3.19 is due for release anyway. I should have a build up for you soon.
Edited 05 Sep 2018 09:38 UTC
Thank you, will wait for v3.19 before I upgrade.
Remco NCrunch Developer
#12633
05 Sep 2018 11:03 UTC
That was quick, let me download and test it.
Tested and it works with v3.19, thank you for the quick turnaround, really appreciated.
Remco NCrunch Developer
#12636
05 Sep 2018 12:07 UTC
Dinesh wrote:Tested and it works with v3.19, thank you for the quick turnaround, really appreciated.
You're welcome :) Thanks for your patience!
Post a reply
Log in to reply.