8

How do you debug/fix a "Common Language Runtime detected an invalid program" error? What exactly does it mean anyway?

I have a C# MVC 2 web app that can deployed to two websites that reside on the same IIS 7.5 webserver (x64). One is the live site (deployed using Release configuration), the second is the beta site (deployed using a new Beta configuration created just for this project).

The two websites are:

Default Website/my_app
Beta/my_app

On the beta site when selecting a paged list of purchase orders, it throws the "detected an invalid program" exception. The exact same code when run on the live site works perfectly. Why would it do this?

Edit: I installed Visual Studio on the server and found the actual line that was causing the problem and the stack trace:

var list = ObjectContext.ObjectSet.AsQueryable();
int totalRecords = list.Count();
var paged = list.Skip((page > 0 ? page - 1 : 0) * rows).Take(rows);

And this is the exception message with stack trace:

{System.InvalidProgramException: Common Language Runtime detected an invalid program.
   at System.Data.Entity.DynamicProxies.PurchaseOrderListVie_96479BFE9FA60F4C53137C56C1A1B2A11D90FF5AFFDC20383CC68E0A750792E3.set_Total(Decimal )
   at lambda_method(Closure , Shaper )
   at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
   at lambda_method(Closure , Shaper )
   at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
   at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at MyApp.Controllers.PurchaseOrderController.GetPurchaseOrderList(Int32 page, Int32 rows, String sidx, String sord) in C:\src\MyApp.2010\MyApp.UI\Controllers\PurchaseOrder\List.cs:line 11}

This new info shows exactly where the problem is, but not what the problem is. Hopefully someone who knows Entity framework very well can shed light on this:

System.Data.Entity.DynamicProxies.PurchaseOrderListVie_96479B_etc.set_Total(Decimal )

Is the line where the error occurs. Now I ran the query in sql management studio and the result was not null, and Total was not null either. So why did it have a problem calling set_Total()?

This is how the POCO defines the Total field (generated by a T4 template):

[Decimal] [Required] [DisplayName("Total")]
public virtual decimal Total
{
    get;set;
}

The main difference between the live and beta sites is the build configuration. But both of the configurations have every single project set to "Any CPU".

All our development machines and servers are 64 bit. Could there be some difference between the IIS configuration of the websites that is causing this?

I've tried running PEVerify - but it just says "All Classes and Methods Verified." How can PEVerify help with this type of problem?

BTW I can see that there are around 15 questions with "Common Language Runtime detected an invalid program problem" in the title. My question is not a duplicate and has several unique features that are different from the other questions that have a similar title (and only one of those 15 is about Entity Framework too - the rest are about Reflection or TFS)

JK.
  • 21,477
  • 35
  • 135
  • 214
  • Visual Studio 2010 supports different web.config files for the different release modes. Have you confirmed that your beta and production config files are functionally identical? – Michael Ames Jul 18 '11 at 21:37
  • Yes the only difference is the database connection string. Both release and beta use the same base web.config and then web deploy to add the correct connection string. – JK. Jul 18 '11 at 21:46
  • Added a +100 bounty to try to get this resolved – JK. Jul 18 '11 at 21:59
  • I'm suspicious of the differing connection strings. Have you tried deploying your production configuration with the beta connection string? – Michael Ames Jul 18 '11 at 22:02
  • @Michael I cant deploy that to production - its the live site. But I'll try deploying the release config to the beta site and then manually changing the config strings to use the beta database. – JK. Jul 18 '11 at 22:09
  • @Michael - nope, same error on beta when I deployed the release config. But when I backup the database from the beta site and restore it on my local dev machine, there is no error (with the same release config). So it must therefore be something about the way the beta site is configured. – JK. Jul 18 '11 at 22:34
  • To be clear: When you deployed the release config to beta, did you use the beta connection string? or production? – Michael Ames Jul 18 '11 at 22:36
  • Okay, I see from your previous comment that it was your plan to test it with the beta connection string. So you've ruled out both the virtual directory and the connection string as culprits. At this point it sounds like you really are down to the build configuration; i.e., the only functional difference between working and not working is the "Debug" vs. "Release" setting?? Mercy... – Michael Ames Jul 18 '11 at 22:40
  • Deployed release config, but then edited it to use the beta DB connection string. I will try release config plus release DB config now. And its not a "Debug" setting, it's a brand new config called "Beta". I wonder if the built in Debug/Release configs have some special properties that manually created configs do not? OK I just tried release config + release db connection deploy to the beta site = same error. – JK. Jul 18 '11 at 22:41

4 Answers4

20

I ran into this issue today when I deployed a web application implementing linq to entity 4 to our QA environment. The issue turned out to be an IIS setting on the Windows 2008 R2 server. Under Advanced Settings for the application pool, the setting for Enable 32-bit Applications was set to False. I set it to True and now my application works just as it did on the development server which happened to be a Windows 2003 server. I hope this helps.

Rick
  • 316
  • 1
  • 2
  • +1 Excellent spotting and looks to have fixed the problem! Now if all of my projects are compiled as Any CPU then what exactly is trying to run as 32 bit? Is Entity Framework itself trying to run as 32 bit? I need to test it fully, but it looks like bounty is going your way :) – JK. Jul 19 '11 at 21:19
  • Thanks. I will certainly appreciate it. I spent a half of a day tracking this one down. – Rick Jul 21 '11 at 21:25
  • Thank you so much for this. I've spent days trying to troubleshoot this when my app runs on some servers but not others. – Nick Jul 29 '17 at 11:27
  • Thanks we had this happen today but in the revers where we had to set 32-bit mode off rather than on as we had done this fix once before. This helped us loads – TechLiam Oct 26 '17 at 08:25
2

For me this happened when I added a view to my entity model. By default the designer sets all the columns Entity Key property to true. When I set it to false for all decimal/numeric columns the error goes away. Tas

Tas
  • 21
  • 1
1

Have you tried to deploy the beta config on another machine? Are your apppools setup to be the same (i.e., classic vs integrated, same .net version)? Have you tried cleaning the solution and redeploying to a new location? Have you tried deploying beta build to release location? (be sure to delete all files before publishing; im curious if theres a leftover dynamically loaded dependency that might be causing problems)

UPDATE:

Excellent wrt more information. On line 3, you are defining the variable page while also using a previously defined variable named page. How does that compile? Try commenting out that code or at least try it without the skip.

NOTE: I think that the Count() followed by the Take() might be executing the query twice.

NOTE2: I've only used the EntityFramework v4 Database First development, but I don't remember programming directly against the ObjectSet. Usually it's your entity class (e.g., MyContext.Orders)... Maybe there is something going on with programming against that object and setting a Decimal value. Are there any properties on the model for that property that make it non-standard?

Jason
  • 4,897
  • 2
  • 33
  • 40
  • App pools are the same, I have tried clean and rebuild and deploy multiple times (and deleting the files before deploying). This happens when I have every project set to deploy as Any CPU so it should not be related to building. Possibly something in Entity Framework? – JK. Jul 18 '11 at 03:22
  • updated the question with more detail. I installed Visual Studio on the server and found the actual line that was causing the problem and the stack trace. – JK. Jul 18 '11 at 05:29
  • Oops - re line #3 thats a typo and should read `paged` - the other var is `int page` which is just the number of the page to retrieve. I'll check out if the count vs take if double executing the query thanks – JK. Jul 19 '11 at 21:18
0

there is a major bug!!, i think it is while having decimal field column property in Entity framework,

i have faced this nasty error msg, i compared getting data from 2 separate tables(same tables),one with an integer column ID and the other with decimal column ID,

for the table with the integer column, no problem same code ran perfectly, for the table with a decimal column, this error message pop out i have made checks on sql server iis and the environment and i even wanted to format the pc, its very weird and the error msg is not clear.

hope this approach helps any facing such an error in the future.

Rabih harb
  • 1,372
  • 12
  • 11