Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidProgramException / Common Language Runtime detected an invalid program

This is the strangest programming issue I have seen in a long time.

I am using Microsoft Visual C# 2010 Express, C# and .NET 2.0 to develop an application. This application references a couple of dll/assemblies (those dlls are all generated on my machine).

Below is part of the code (it is all basic stuff):

public class PowerManagement
{

    [TestCase]
    public void PrepareTest(){
        // Configure according to pre-conditions
        Preconditions precondition = new Preconditions();
        precondition.SetupPreconditions();
            ...
    }

    [TestCase]
    public void PerformTest(){
        TestcaseData testcaseData = new TestcaseData();

        // Set Trigger and perform check
        switch (testcaseData.triggerNumber){
            case (1):
                if ((new Trigger1(testcaseData)).Validate() != 1)
                    Report.TestStepFail("failed");
                break;
            ...
            case (4):
                if ((new Trigger4(testcaseData)).Validate() != 1)
                    Report.TestStepFail("failed");
                break;
            default:
                Report.TestStepFail("Not yet implemented");
                break;
        }
    }
}

This application is then generated into a dll from Visual C# 2010 Express and used elsewhere and all is fine. The problem surfaces when I add another case to the switch-statement above (see below)

        ...
        case (4):
            if ((new Trigger4(testcaseData)).Validate() != 1)
                Report.TestStepFail("failed");
            break;
        case (5):
            if ((new Trigger5(testcaseData)).Validate() != 1)
                Report.TestStepFail("failed");
            break;
        default:
            Report.TestStepFail("Not yet implemented");
            break;

I can still build without a single issue and generate the dll but when I use the generated dll I get the following error:

A .NET exception (InvalidProgramException) occured in the module PowerManagement
Error message: Common Language Runtime detected an invalid program.
Throwing method: PowerManagement.PerformTest

(the issue happens even if I copy case(4) and paste it as a new case, so it has nothing to do with Trigger5-class)

What is happening here? I have looked through the other InvalidProgramException and Common Language Runtime in Stackoverflow but none seemed related.

I know this issue is strange so please let me know and I will provide more information. I am using a 64-bit Windows 8 machine, if that matters. I have already checked for any updates on VS and .NET updates. I havet also regenerated all the dlls a couple of time ans also created the solution from scratch a couple of times.

like image 813
theAlse Avatar asked May 09 '14 11:05

theAlse


3 Answers

Just wanted to add my experience for this... In my case, I am hosting my C# Web API on Azure and I encountered this message when trying to log in to my API. I had to go into my Azure management portal (portal.azure.com), go to App Services, choose my Web API program and click Restart from the Overview screen. After this, the program worked as normal again. Did not find any further clues in my logs.

like image 80
slasky Avatar answered Oct 06 '22 23:10

slasky


Try enabling 32-bit applications in your application pool advanced settings.

like image 8
algiecas Avatar answered Oct 06 '22 22:10

algiecas


I finally managed to solve this issue. I unchecked code optimization in C# Express and that solved the issues. Still the weirdest thing, but since we are using old tools and framework we can not really blame anyone.

like image 7
theAlse Avatar answered Oct 06 '22 22:10

theAlse