none
Moving to Windows 8 get "InvalidProgramException: Common Language Runtime detected an invalid program" RRS feed

  • Question

  • Hello 

    I'm developing a .NET 4.0 application with AutoMapper(https://github.com/AutoMapper) on Windows 7. Today I get the source code from SVN to a new Windows 8 machine, build and then run the app, it throws me "InvalidProgramException: Common Language Runtime detected an invalid program". I rebuild the source codes and it runs well on all other three Windows 7 machines. I guess it does matter with Windows 8. I write the following small console application.  I build the source code on Windows 8 and it does run well on Windows 7 but doesn't on Windows 8. So it's something about the runtime on Windows 8. It seems only something wrong with the enum type whose underlying type is not the default type, Int32. 

    After googling, people say it's about JIT compiler but I still have no idea. I use VS 2010 targeted .NET 4.0 on both Windows 7 and Windows 8. Does any change about this in Windows 8? I'm reading the Source code of AutoMapper. I think it really something wrong with C# or CLI. Hope you give some clue. Thanks.

        class Program
        {
            enum PriorityModel  { High, Normal, Low }
            enum PriorityDto { High, Normal, Low }
    
            enum PriorityModel2 : byte { High, Normal, Low }
            enum PriorityDto2 : byte { High, Normal, Low }
    
            enum PriorityModel3 : short { High, Normal, Low }
            enum PriorityDto3 : short { High, Normal, Low }
    
            
            static void Main(string[] args)
            {
                Mapper.CreateMap<PriorityModel, PriorityDto>();  // OK
                Mapper.CreateMap<PriorityModel2, PriorityDto2>();  // [InvalidProgramException: Common Language Runtime detected an invalid program.]
                Mapper.CreateMap<PriorityModel3, PriorityDto3>();  // [InvalidProgramException: Common Language Runtime detected an invalid program.]
    
                Console.Read();
            }
        }

    exception stacktrace:

    [InvalidProgramException: Common Language Runtime detected an invalid program.]
       System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method) +0
       System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target) +43
       System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate() +81
       System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator) +205
       AutoMapper.DelegateFactory.CreateGet(FieldInfo field) +156
       AutoMapper.Internal.FieldGetter..ctor(FieldInfo fieldInfo) +60
       AutoMapper.ReflectionHelper.ToMemberAccessor(MemberInfo accessorCandidate) +124
       AutoMapper.TypeMapFactory.CreateTypeMap(Type sourceType, Type destinationType, IMappingOptions options, MemberList memberList) +269
       AutoMapper.ConfigurationStore.CreateTypeMap(Type source, Type destination, String profileName, MemberList memberList) +86
       AutoMapper.ConfigurationStore.CreateMap(String profileName, MemberList memberList) +45
       AutoMapper.ConfigurationStore.CreateMap() +13
       AutoMapper.Mapper.CreateMap() +51

    问题要简单,错误须详细@错误/异常/堆栈信息+操作系统+软件版本+all the context of the issue Hope Helpful | http://www.leoworks.net

    Monday, September 3, 2012 3:38 PM

All replies

  • Hello 

    I'm developing a .NET 4.0 application with AutoMapper(https://github.com/AutoMapper) on Windows 7. Today I get the source code from SVN to a new Windows 8 machine, build and then run the app, it throws me "InvalidProgramException: Common Language Runtime detected an invalid program". I rebuild the source codes and it runs well on all other three Windows 7 machines. I guess it does matter with Windows 8. I write the following small console application.  I build the source code on Windows 8 and it does run well on Windows 7 but doesn't on Windows 8. So it's something about the runtime on Windows 8. It seems only something wrong with the enum type whose underlying type is not the default type, Int32. 

    After googling, people say it's about JIT compiler but I still have no idea. I use VS 2010 targeted .NET 4.0 on both Windows 7 and Windows 8. Does any change about this in Windows 8? I'm reading the Source code of AutoMapper. I think it really something wrong with C# or CLI. Hope you give some clue. Thanks.

        class Program
        {
            enum PriorityModel  { High, Normal, Low }
            enum PriorityDto { High, Normal, Low }
    
            enum PriorityModel2 : byte { High, Normal, Low }
            enum PriorityDto2 : byte { High, Normal, Low }
    
            enum PriorityModel3 : short { High, Normal, Low }
            enum PriorityDto3 : short { High, Normal, Low }
    
            
            static void Main(string[] args)
            {
                Mapper.CreateMap<PriorityModel, PriorityDto>();  // OK
                Mapper.CreateMap<PriorityModel2, PriorityDto2>();  // [InvalidProgramException: Common Language Runtime detected an invalid program.]
                Mapper.CreateMap<PriorityModel3, PriorityDto3>();  // [InvalidProgramException: Common Language Runtime detected an invalid program.]
    
                Console.Read();
            }
        }

    exception stacktrace:

    [InvalidProgramException: Common Language Runtime detected an invalid program.]
       System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method) +0
       System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target) +43
       System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate() +81
       System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator) +205
       AutoMapper.DelegateFactory.CreateGet(FieldInfo field) +156
       AutoMapper.Internal.FieldGetter..ctor(FieldInfo fieldInfo) +60
       AutoMapper.ReflectionHelper.ToMemberAccessor(MemberInfo accessorCandidate) +124
       AutoMapper.TypeMapFactory.CreateTypeMap(Type sourceType, Type destinationType, IMappingOptions options, MemberList memberList) +269
       AutoMapper.ConfigurationStore.CreateTypeMap(Type source, Type destination, String profileName, MemberList memberList) +86
       AutoMapper.ConfigurationStore.CreateMap(String profileName, MemberList memberList) +45
       AutoMapper.ConfigurationStore.CreateMap() +13
       AutoMapper.Mapper.CreateMap() +51



    问题要简单,错误须详细@错误/异常/堆栈信息+操作系统+软件版本+all the context of the issue Hope Helpful | http://www.leoworks.net



    • Edited by Xiaofeng Wang Monday, September 3, 2012 1:55 PM
    • Moved by Bob Shen Tuesday, September 4, 2012 3:02 AM (From:Visual C# General)
    • Merged by Mike FengModerator Friday, September 7, 2012 8:17 AM duplicate
    Monday, September 3, 2012 12:35 PM
  • It's a problem with automapper.  I'm running all sorts of .NET 4 apps on Win8 and having no problems.  I notice that AutoMapper is generating an exception during reflection.  Do you happen to have .NET 4.5 installed?   If so then note that it updates the v4 CLR and with that update many apps that use reflection break.  I've seen it with Reflector and JustDecompile.  They get back data they aren't expecting from reflection and blow up.  I suspect that is what is happening here.  I recommend that you debug the AM code to see what is going on.

    Michael Taylor - 9/3/2012
    http://msmvps.com/blogs/p3net

    Monday, September 3, 2012 3:55 PM
    Moderator
  • Hello Michael Thanks for your rely.

    As I know, .NET 4.5 is shipped with Win8 and I indeed install VS 2012 on the Win8 machine. The method throwing the exception in AutoMapper as following, it seems normally be programming with Reflection and lambda expression:

    public static LateBoundFieldGet CreateGet(FieldInfo field)
    {
    ParameterExpression instanceParameter = Expression.Parameter(typeof(object), "target");

    MemberExpression member = Expression.Field(Expression.Convert(instanceParameter, field.DeclaringType), field);

    Expression<LateBoundFieldGet> lambda = Expression.Lambda<LateBoundFieldGet>(
    Expression.Convert(member, typeof(object)),
    instanceParameter
    );

    return lambda.Compile();
    }



    问题要简单,错误须详细@错误/异常/堆栈信息+操作系统+软件版本+all the context of the issue Hope Helpful | http://www.leoworks.net

    Monday, September 3, 2012 4:32 PM
  • I just grabbed a copy from NuGet and ran your code through it and it worked fine.  Do you have the latest version and are you recompiling your code?
    Monday, September 3, 2012 5:19 PM
    Moderator
  • Strange! I really have the latest AutoMapper 2.1.267 from NuGet. My Win8 is RTM, Microsoft Windows [Version 6.2.9200].  Do you run it well on Win8? 

    问题要简单,错误须详细@错误/异常/堆栈信息+操作系统+软件版本+all the context of the issue Hope Helpful | http://www.leoworks.net

    Monday, September 3, 2012 5:58 PM
  • I don't run it at all.  I downloaded it from NuGet to test the code you posted.  That's the only time I've done it.  I tried using both .NET 4 and .NET 4.5.

    Michael Taylor - 9/3/2012
    http://msmvps.com/blogs/p3net

    Tuesday, September 4, 2012 2:35 AM
    Moderator
  • I mean weather you run the exe built from the above source code on Windows 7 or Windows 8? I really just download the lastest AutoMapper from NuGet and build the above source code with it in VS 2010 on Windows 8. The exe crash on Windows 8 but run fine on Windows 7. 

    As I know, .NET 4.5 is shipped with WIN8 and .NET 4.5 is in-place upgrade from .NET 4. So even I have install VS2010 on WIN8, I only have .NET 4.5 runtime on Win8.  My WIN7 is only installed with .NET4 without .NET 4.5. Thus I guess something changed in .NET 4.5 breaks the AutoMapper originally based on .NET 4.0.


    问题要简单,错误须详细@错误/异常/堆栈信息+操作系统+软件版本+all the context of the issue Hope Helpful | http://www.leoworks.net

    Tuesday, September 4, 2012 3:11 AM
  • Michael, could you kindly send me (xfwang0724(a)gmail.com) a copy of your testing source code solution or the testing exe? I would like to test it on my Win7 and Win8. Thanks a lot.

    问题要简单,错误须详细@错误/异常/堆栈信息+操作系统+软件版本+all the context of the issue Hope Helpful | http://www.leoworks.net


    Tuesday, September 4, 2012 3:13 AM
  • Hi XiaoFeng,

    Thanks for posting on this forum.

    This code can reproduce on your side, but not on my side, please design a sample without the third party tools.

    Thank you.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, September 4, 2012 8:59 AM
    Moderator
  • I'll send it tonight but it is just a copy of your program.
    Tuesday, September 4, 2012 2:20 PM
    Moderator
  • Your e-mail bounced back as undeliverable.
    Thursday, September 6, 2012 3:09 AM
    Moderator
  • Hmm. It's really xfwang0724@gmail.com . Could your try again? And please send me the exe. I will run the exe on my Win8 to see the result. Thanks.


    问题要简单,错误须详细@错误/异常/堆栈信息+操作系统+软件版本+all the context of the issue Hope Helpful | http://www.leoworks.net

    Thursday, September 6, 2012 8:14 AM
  • Hi Xiaofeng,

    How about your issue now?

    Did you have solution or workaround?

    Would you like to sharing it here?

    Thank you.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, September 24, 2012 2:46 PM
    Moderator
  • I've got the same exception after installing win8. Here is a quick repro:

    [TestClass()]
    public class ProgramTest
    {
        [TestMethod()]
        public void MainTest()
        {
            ServiceHost serviceHost = new ServiceHost(typeof(TestService));
        }
    }
    
    public class TestService { }

    The same exception on another win8 x64. On win7 with the same .net framework version (4.5.50709) it works fine.

    Here is the environment:

    win8 x64 enterprise (6.2.9200)

    vs2010 (10.0.40219.1) SP1Rel, .net framework 4.5.50709 SP1Rel

    Hope it helps.

    Tuesday, November 20, 2012 12:12 PM
  • I don't believe your problem is the same as the OP.  Please post your question in the testing forum.  Your test code doesn't look like it should run at all because you're using WCF with non-WCF.  Your problem more likely stems from the fact that you're mixing platforms between testing framework and code.  The testing forum folks will be able to better help you out with this particular issue.

    Michael Taylor - 11/20/2012
    http://msmvps.com/blogs/p3net

    Tuesday, November 20, 2012 7:15 PM
    Moderator
  • I didn't say this test would complete. My tests started failing with InvalidProgramException after I installed win8. And that was just repro of the exception which shouldn't be raised in this case

    The exception that is thrown when a program contains invalid Microsoft intermediate language (MSIL) or metadata. Generally this indicates a bug in the compiler that generated the program

     This test doesn't run on win7 also, but it fails with correct exception. And on win8 with target .net 3.5 it throws normal exception, and even on win7 with .net 4.5 it throws normal exception.

    that's the similarity with Xiaofeng's case. Do you really think the cause is different?

    Tuesday, November 20, 2012 9:26 PM
  • Without replicating the issue I can't say fully.  I would recommend taking your generated test assembly and disassembling it to see if it is valid IL.  If you're not using MSTest then I'd also recommend switching to that temporarily to see if it is a test execution problem.  Additionally I'd verify whether it occurs in x86 and/or x64 mode.

    Michael Taylor - 11/20/2012
    http://msmvps.com/blogs/p3net

    Wednesday, November 21, 2012 3:04 AM
    Moderator