Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
"Operation could destabilize the runtime" from Casting from Concrete to Interfaces with Linq

In all my years of doing .Net I have to say that I finally came across the exception message that takes the cake.  Today while trying to do some refactoring on the DimeCasts.net code base I received this exception.

Operation could destabilize the runtime

I received the error while trying to update some Linq-2-Sql code that where I was trying to return the data as IList<IDownloadInformation> (yes I killed the delayed execution).  Below is the code that caused the exception

public IList<IDownloadInformation> FetchEpisodeDownloads( Int32 episodeID )
{
	var result = ( from d in DBContextInstance().EpisodeDownloadInformations
        	where d.EpisodeID == episodeID
		select (IDownloadInformation)new DownloadInformation()
                	{
                        DownloadID = d.ID,
                        EpisodeID = d.EpisodeID,
                        ...
                        
                       	);

	return (IList<IDownloadInformation>) result.ToList();
}

Turns out the above code works, mostly.  Everything is fine until I went to cast the return results.  It was then I received the nice exception.

As soon as I found the error I hit Google for some help. I was able to find this post which helped to point me in the right direction. 

After taking a look at my code again, I was able to get the code below to work (this time I decided to use IQueryable so I could get the delayed execution.

        
public IQueryable<IDownloadInformation> FetchEpisodeDownloads( Int32 episodeID )
{
	var result = ( from d in DBContextInstance().EpisodeDownloadInformations
        	where d.EpisodeID == episodeID
                	select (IDownloadInformation)new DownloadInformation()
                        {
                        	DownloadID = d.ID,
                                EpisodeID = d.EpisodeID,
                                ..                                 
			}
			);

	return result.OfType<IDownloadInformation>();
}

Using the built in OfType support to cast your return value back to the interface seemed to work and it is pretty clean and simple.  What is funny about the actual exception that was thrown is that it makes no sense and really does not lead you to the problem.  Gotta love Google :)

Hope this helps.

Till next time,

[--- Check out www.Dimecasts.net ---]


Posted 11-29-2008 10:19 AM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

jdn wrote re: "Operation could destabilize the runtime" from Casting from Concrete to Interfaces with Linq
on 11-29-2008 5:58 PM

That's awesome.  Since I tend to use IQueryable by default, I haven't hit that error message, but I love the wording.

Imagine if you could create your own error messages, so that, for instance, if someone used nested if-else statements to too deep a level:

"Use of this code will destabilize maintainability."

Dew Drop - Weekend Edition - November 29-30, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - Weekend Edition - November 29-30, 2008 | Alvin Ashcraft's Morning Dew
on 11-30-2008 11:33 AM

Pingback from  Dew Drop - Weekend Edition - November 29-30, 2008 | Alvin Ashcraft's Morning Dew

Reflective Perspective - Chris Alcock » The Morning Brew #234 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #234
on 12-01-2008 5:50 AM

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #234

Nick berardi wrote re: "Operation could destabilize the runtime" from Casting from Concrete to Interfaces with Linq
on 12-01-2008 9:49 AM

+1 to Microsoft for best and most scariest exception message.

sergiopereira wrote re: "Operation could destabilize the runtime" from Casting from Concrete to Interfaces with Linq
on 12-01-2008 2:48 PM

This reminds me of an old friend from my COM/COM+ days:

"The callee (server [not server application]) is not available and

disappeared; all connections are invalid. The call may have executed."

I'd add "... or maybe not". Bonus point for the ( ) and [ ] nesting.

on 12-02-2008 8:50 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Casey wrote re: "Operation could destabilize the runtime" from Casting from Concrete to Interfaces with Linq
on 06-08-2009 7:52 PM

Thank you so much for the post. Solved my problem.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)