InvalidProgramException / Common Language Runtime detected an invalid program

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.


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


I had this problem after upgrading to Visual Studio 2017 v15.8.6. The problem went away when I removed the assemblyPostProcessorType attribute in the compilation tag in web.config.


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.