2

We are using MEF to customize certain functionality of our ASP MVC application. We are using .NET 4.0, and SQL Server 2008. We have run into some very strange behavior when using EF4 and a LINQ statement to a view inside a MEF assembly.

The code below will timeout when executed as part of the imported assembly using MEF.

It will run in less than a second when it is pulled out and executed in the main assembly.

If the date range is set to one day the MEF implementation will eventually finish in about 15 seconds.

If we use a table instead of a view it will work fine in the MEF implementation <1 sec.

The code below and the view works flawlessly everywhere except when executed inside the MEF code.

Does MEF introduce any special limitations or problems when EF4 is used in a MVC Web application? We can find a work-around to this problem, but I'd like to know if this is a symptom that we are using MEF wrong?

DateTime startDate = DateTime.Now.AddDays(-30);
DateTime endDate = DateTime.Now;

var _Items = dto.ClientContext.vwItemLists.Where(c => c.CreatedOn > startDate && c.CreatedOn <= endDate);

_context.TotalItemsMatched = _Items.Count();
tereško
  • 58,060
  • 25
  • 98
  • 150
  • Turns out MEF was not the culprit, but rather the SQL view. The view is, and remains fast (less than 2) seconds, except when EF is executing it. It also depends on where the execution takes place. Inside MEF was reliably timing out, but we also got it to time out as we moved it around. We have confirmed that this is not a locking issue, and not an execution plan issue. The only suspect is EF4. We stopped using the view and instead built a LINQ statement that does the same thing. This works. – Per Cedersund May 18 '11 at 15:58

1 Answers1

0

The problem is likely occurring due to a bad cached execution plan. See this similar issue.

This answer has worked for me in the past, when I'm pretty sure some sort of one-time corruption was the culprit, but note the comment that this is a short-term fix and in many cases the issue might crop up again if you don't resolve the root of the problem.

He refers to this article, which is unfortunately pretty heavy reading, to help you try to identify why a bad execution plan was cached in the first place.

Community
  • 1
  • 1
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315