What could cause this casting error?

Error:

Unable to cast object of type ‘System.Collections.Generic.List`1[EditQuoteController+UnsavedQuoteItemInfo]’ to type ‘System.Collections.Generic.List`1[EditQuoteController+UnsavedQuoteItemInfo]’.

Yes, you read that right.  Unable to cast object of type X to type X.

I have seen this type of error once before and it was when there were multiple assemblies referencing different version of a common assembly, so even though their names were the same, their versions were different.

But in this instance there is nothing like this that could be having an impact.  All of the classes of consequence are in the same assembly.

Also, I don’t get exception very often, only every now and then.

UPDATE:

I think maybe this is happening between builds on my development machine b/c I am storing some data in the session.  So the version in session was from the last build?  Doesn’t sound like a great explanation, but it’s the best I have at this point.

UPDATE 2:

I am pretty sure that what I wrote in the last update is what is actually happening. 

I used this code:

Try
    list = CType(view.Session.Item(Me.UnsavedKey), List(Of UnsavedQuoteItemInfo))
Catch ex As System.InvalidCastException
    Dim sError As String = "Unable to cast from type " & _
        view.Session.Item(Me.UnsavedKey).GetType.AssemblyQualifiedName & _
        " to type " & GetType(List(Of UnsavedQuoteItemInfo)).AssemblyQualifiedName & _
        ".  The session has been cleared."
    view.Session.Item(Me.UnsavedKey) = Nothing
    Throw New System.ApplicationException(sError)
End Try

Which producted the following 2 types:

System.Collections.Generic.List`1[[EditQuoteController+UnsavedQuoteItemInfo, App_Web_bp-bbqew, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral PublicKeyToken=b77a5c561934e089

System.Collections.Generic.List`1[[EditQuoteController+UnsavedQuoteItemInfo, App_Web_ve7ziow-, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089. 

I am guessing this is a by product of using the web site project, instead of web application project.  There are some other reasons why we wanted to use web site instead of web app on this project, so you don’t have to scold me: we made the right choice.

But it seems that every time I make a change to the project, it creates a new dynamically named assembly.  So even though the classes are really the same, they are globally different as far as asp.net is concerned.

 

WebAii Website Automated Testing Framework

I am always on the lookout for better and easier ways to automate testing of my applications.  Mostly, this stems from my teams not being too keen on implementing testing, so the easier I can make it, the easier it will be to convince others to write tests.

So Phil Haack has suggested a free framework called WebAii, and after taking a quick look, it looks promising.

It supports some nice features like mouse/keyboard actions for Ajax testing, and dom actions (find an element and click it, or whatever).  It also supports unit testing your javascript functions by having your test call the functions.  It also integrates with Nunit.  Nice!

Hopefully I can find some free time (HAHAHHAAH) when I can test this out more in a project.

 

Free Flyout and Alternating Panel Controls

These are some nice looking, cross browser compliant, ASP.NET based, free controls from Obout.com

Flyout can perform stuff like this:

But almost more interesting is how it can be used in conjunction with traditional controls.  For example, you can wire up a nice looking “Alt Text” effect for images and labels, and you can provide some nice explanation in the flyout for how to properly fill in a control.  The example they give on their site shows a textbox for “Routing Number” and when you click on the textbox it shows this in the flyout:

 

 

The alternating content control which is called “Show” (Show examples) can rotate through some content like so:

These are both free controls.  Very nice!

 

ASP.NET Application Not Reading The Web.Config File

I recently ran into an interesting problem… my webservice application seemed unable to read info from the web.config file.

I tried adding some invalid < marks to the config file and the app still ran w/o any error (but still wouldn’t read the web.config appSettings or connectionString sections).

So I created another IIS application and it worked as expected.

So I deleted the troubled IIS App and recreated it.  Still broken!

The solution was to clear out the ASP.NET Temporarly Files folder in c:windows……

Once that was gone, and I restarted IIS, everything went back to normal.

 

Solution for "Thread was being aborted" exception when you call Response.End (or .Redirect)

You’ve probably seen this one.

Whenever you do one of the following:

Response.End()
Response.Redirect("page.aspx")
Server.Transfer("page.aspx")

You end up with a ThreadAbortException, “Thread was being aborted”.

I had previously dealt with this by swallowing the ThreadAbortException, which of course isn’t a great method, but it worked.

Well today I came across a better way for all of these.

Replace This With This
Response.End HttpContext.Current.ApplicationInstance.CompleteRequest
Response.Redirect(“page.aspx”) Response.Redirect(“page.aspx”,false)
Server.Transfer(“page.aspx”) Server.Execute(“page.aspx”)

ScottGu Demos Upcoming MVC Framework for ASP.NET

In a recent gathering of the ALT.NET group, ScottGu gave a demo of the upcoming MVC framework for asp.net.

The article (and video) can be found here.

Lots of people in the ALT community have been working with asp.net and MVC by using one of the OS frameworks out there like Monorail, but I am glad to see that MS is not sitting around waiting on this issue.

Hopefully this will make testing even easier. 

404s on ASP.NET AJAX script files in the System.Web.Extensions folder

Recently I ran into a problem where browsing to a newly installed web app produced a bunch of javascript errors.  Stuff like: “‘Type’ is not defined” and “‘Sys’ is not defined”.

After debugging it for a while, I found the problem to be that URLScan had been installed on the server (Windows 2000 Server), which was preventing any requests with dots in the folder name.

URL Scan is a tool that MS suggested everyone install a while back that acts to filter out many malicious attacks.

So, with the default settings any request for a file inside the scriptsSystem.Web.Extensions folder would be denied as a 404 b/c of URL Scan.

To fix this, you need to edit the UrlScan.ini file, located in %WINDIR%System32InetsrvURLscan.  Near the top of the file, change AllowDotInPath from 0 to 1.

The run iisreset to restart IIS and you should be ok!

More info on URLScan is available here:

http://support.microsoft.com/kb/326444

 

Subsonic MVC Templates. Not what I was expecting.

When I saw a new item in my RSS feed from Rob Conery about MV* I was immediately interested to read it, because I have been working on trying to create my web app pages using MVP, but am unable to find any examples beyond the most basic.

I would love to see how other people manage the interactions between the Controller and the View, to see how it compares to how I am doing it. 

My view interfaces tend to be kinda large.  For example, if I have a button that I hide and show depending on business rules, I will create a MyButtonVisibility property on the interface can set the properties from the controller.

I would be interested to see how others deal with things like the hiding / showing of items.  I could see wrapping more of that kind of functionality in the view, and giving the view some more logic but I think you would then start to lose some of the testability.

Anyway, the articl on Rob’s blog was really to talk about creating an MVC style architecture for subsonic itself, not the pages that use it.  However, Rob seemed to suggest that the new changes would aid you in using MV* in your pages by forcing you into good habits.

But I really don’t understand how that would work.  If you have code that does:

MyGridView.DataSource=Product.FetchAll();
MyGridView.DataBind();

And you change it so that you use a Controller (or Manager as I have called it when loading Business Objects or DTOs) to look like this:

Product product = ProductController.Get(newID);
product.ReorderLevel = 100;
ProductController.Save(product,"unit test");

I don’t see how this helps you create an MV* architecture in your pages.

Maybe I am just not understanding.