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 ---]