Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Entity Framework 4 Common Language Runtime detected an invalid program error?

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)

like image 213
JK. Avatar asked Jul 13 '11 02:07

JK.


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


3 Answers

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.

like image 148
Rick Avatar answered Oct 19 '22 07:10

Rick


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

like image 33
Tas Avatar answered Oct 19 '22 08:10

Tas


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?

like image 1
Jason Avatar answered Oct 19 '22 09:10

Jason