Thursday, January 19, 2006
« Go Live licenses for WCF and WF | Main | MIX 06 - Microsoft Web Conference »

In a previous post I discussed some issues I’ve been having with the ASP.NET Development Server (aka VS Host or Cassini).

 

I have more information direct from Microsoft on my issue. It turns out that it is “by design”, and a sad thing this is… VS Host is designed such that the thread on which your code runs can (and does) go between AppDomains.

 

Objects placed on the Thread object, such as the CurrentPrincipal, must be serializable and the assembly must be available to all AppDomains; even the primary AppDomain that isn’t running as part of your web site!

 

And this is the root of my problem. I create a custom IPrincipal object in an assembly (dll). I put it in the Bin directory and then use it – which of course means it ends up on the Thread object. Cassini then runs my code in an AppDomain for my web site and all is well until it switches out into another AppDomain that isn’t running my web site (but rather is just running Cassini itself). Boom!

 

Why boom? Well, that custom IPrincipal object on the thread is still on the thread. When the thread switches to the other AppDomain, objects directly attached to the thread (like CurrentPrincipal) are automatically serialized, the byte stream transferred to the new AppDomain, and deserialized into the new AppDomain. This means that the new AppDomain must have access to the assembly containing the custom IPrincipal class – but of course it doesn’t, because it isn’t running as part of the web site and thus doesn’t have access to the Bin directory.

 

What’s the answer? Either don’t use Cassini (which has been my answer), or install the assembly with the custom IPrincipal into the GAC. Technically the latter answer is the “right” one, but that has the ugly side-effect of preventing rapid application development. All of a sudden you can’t just change a bit of code and press F5 to test; instead you must build your code, update the GAC and then you can test. Nasty…

 

As an aside, this is exactly the same issue you’ll run into when using nunit to test code that uses a custom IPrincipal on the thread. Unlike nunit however, you can’t predict when Cassini will switch you to another AppDomain so you can’t work around the issue by clearing the CurrentPrincipal like you can with nunit (or at least I haven’t found the magic point at which to do it…).

 

What’s really scary is that it was implied that this could happen under IIS as well – but that flies in the face of years of experiential evidence to the contrary. I guess the safe thing to do is to treat IIS like Cassini, and put shared assemblies in the GAC. But I’m not sure I’m ready to advocate that yet, because that means complicating installs a whole lot, and I’ve never encountered this threading issue under IIS so I don’t think it is a real issue.

Thursday, January 19, 2006 9:34:17 AM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [18]  |  Related posts:
Good post on C#/VB co-evolution
nunit to mstest for CSLA .NET 4
Magenic’s own Code Master
DataAnnotations not in the client profile
It is only 8, how can it be legacy???
Windows 7 features in .NET
Tracked by:
"bay area photographer" (bay area photographer) [Trackback]
"maryland automotive windshield" (maryland automotive windshield) [Trackback]
"her first audition" (her first audition) [Trackback]
"annuncio affitti isernia" (annuncio affitti isernia) [Trackback]
"paul posey tallahassee florida" (paul posey tallahassee florida) [Trackback]
"sauna mista milano" (sauna mista milano) [Trackback]
"georgia bulldog" (georgia bulldog) [Trackback]
"cd organizers" (cd organizers) [Trackback]
"congelatore da incasso" (congelatore da incasso) [Trackback]
"cameriere in autoreggenti" (cameriere in autoreggenti) [Trackback]
"Sports Betting Rss Feed" (Sports Betting Rss Feed) [Trackback]
"albuquerque respiratory jobs va medical center" (albuquerque respiratory jobs v... [Trackback]
"tiaras and headpieces" (tiaras and headpieces) [Trackback]
"used kountry aire camper trailer" (used kountry aire camper trailer) [Trackback]
"columbus ohio commercial real estate" (columbus ohio commercial real estate) [Trackback]
"invisibile pulcino dildo" (invisibile pulcino dildo) [Trackback]
"fotografico" (fotografico) [Trackback]
"sms gratis cellulare" (sms gratis cellulare) [Trackback]
"racconto erotici con animali" (racconto erotici con animali) [Trackback]
"female body builders posing" (female body builders posing) [Trackback]
"zyban side affects" (zyban side affects) [Trackback]
"Socialist Appeal" (Socialist Appeal) [Trackback]
"Untitled Document" (Untitled Document) [Trackback]
"Over N Out. :: Index" (Over N Out. :: Index) [Trackback]
"Galaxy Portfolio Services" (Galaxy Portfolio Services) [Trackback]
"fuck that, fuck them, fuck everyone else." (fuck that, fuck them, fuck everyone... [Trackback]
http://www.setfirestudios.com/work.php [Trackback]
"4839_architectonic - Departurebanish.com" (4839_architectonic - Departurebanish... [Trackback]
http://www.setfirestudios.com/main.php [Trackback]
"Untitled Document" (Untitled Document) [Trackback]
"backsoon" (backsoon) [Trackback]
"halifest" (halifest) [Trackback]
"Baylor - Departurebanish.com" (Baylor - Departurebanish.com) [Trackback]
"Untitled Document" (Untitled Document) [Trackback]
"Running The Guard... NYHC" (Running The Guard... NYHC) [Trackback]
"4 < 9" (4 < 9) [Trackback]
"AlloyMail" (AlloyMail) [Trackback]
"Non-party - Departurebanish.com" (Non-party - Departurebanish.com) [Trackback]
"CHARA. reppin' pei" (CHARA. reppin' pei) [Trackback]
"8533 - Departurebanish.com" (8533 - Departurebanish.com) [Trackback]
"Erica Funghi Snc" (Erica Funghi Snc) [Trackback]
"2029_guiftes - Departurebanish.com" (2029_guiftes - Departurebanish.com) [Trackback]
"Law-unto-itself - Departurebanish.com" (Law-unto-itself - Departurebanish.com) [Trackback]
"Circumstances_8558 - Departurebanish.com" (Circumstances_8558 - Departurebanish... [Trackback]
"Deutsche Lebens Rettungs Gesellschaft eV (DLRG)" (Deutsche Lebens Rettungs Gese... [Trackback]
"Athenian_622 - Departurebanish.com" (Athenian_622 - Departurebanish.com) [Trackback]
"Suggestion_2469 - Departurebanish.com" (Suggestion_2469 - Departurebanish.com) [Trackback]
"Bus Pass" (Bus Pass) [Trackback]
"Visual Elements: The Alkaline Earth Metals" (Visual Elements: The Alkaline Eart... [Trackback]
"Departurebanish.com" (Departurebanish.com) [Trackback]
"She Kills dot com bitches." (She Kills dot com bitches.) [Trackback]
"Camera Whores!!!" (Camera Whores!!!) [Trackback]
"Untitled Document" (Untitled Document) [Trackback]
"Untitled Document" (Untitled Document) [Trackback]
"1471 - Departurebanish.com" (1471 - Departurebanish.com) [Trackback]
http://www.setfirestudios.com/contacts.php [Trackback]
"Untitled Document" (Untitled Document) [Trackback]
"electric tool accessories" (electric tool accessories) [Trackback]
"legal aid society" (legal aid society) [Trackback]
"posing stool" (posing stool) [Trackback]
"foto ose" (foto ose) [Trackback]
"second income ideas" (second income ideas) [Trackback]
"good looking idraulico" (good looking idraulico) [Trackback]
"alternative school programs" (alternative school programs) [Trackback]
"Force Outboard Motor Parts" (Force Outboard Motor Parts) [Trackback]
"hilo hawaii real estate" (hilo hawaii real estate) [Trackback]
"custom plaque drop ship" (custom plaque drop ship) [Trackback]
"snowmobile helmets" (snowmobile helmets) [Trackback]
"aol and vpn connections" (aol and vpn connections) [Trackback]
"fictional detectives" (fictional detectives) [Trackback]
"Kawasaki Ninja 250 Performance" (Kawasaki Ninja 250 Performance) [Trackback]
"enoteche" (enoteche) [Trackback]
"bakersfeild ca" (bakersfeild ca) [Trackback]
"panasonic plasma tv" (panasonic plasma tv) [Trackback]
"print manager software" (print manager software) [Trackback]
"ridiculous infermiera azione" (ridiculous infermiera azione) [Trackback]
"bitch island" (bitch island) [Trackback]
"chicago bbw escort" (chicago bbw escort) [Trackback]
"challenger gate garage door openers" (challenger gate garage door openers) [Trackback]
"building developers IN USA" (building developers IN USA) [Trackback]
"handbags and gladrags stereophonics" (handbags and gladrags stereophonics) [Trackback]
"Ford 56 F100 for Sale" (Ford 56 F100 for Sale) [Trackback]
"Santa Barbara Collection" (Santa Barbara Collection) [Trackback]
"bangkok train schedule" (bangkok train schedule) [Trackback]
"ford warranty" (ford warranty) [Trackback]
"Luggage Repair Parts %2B wheels" (Luggage Repair Parts %2B wheels) [Trackback]
"aspirin vs plavix" (aspirin vs plavix) [Trackback]
"real estate woodstock georgia" (real estate woodstock georgia) [Trackback]


Thursday, January 19, 2006 12:00:10 PM (Central Standard Time, UTC-06:00)
Maybe not ideal, but what about a post-build script to add the assembly with the custom IPrincipal to the GAC?
Monday, January 23, 2006 8:40:49 AM (Central Standard Time, UTC-06:00)
Hey.. same problem here.. is there a way of doing it in Global.asax? I have a web site and it has an App_Code folder... the assembly name for classes in that folder are unfortunately randomly generated .. App_Code.xzhasd112 or something.

Maybe using reflection and adding a few lines in Application Start or such can do it?

p.s. my error message is "SerializationException: Type is not resolved for member..."

I'm pretty sure its the same thing... it can't find the type when it deserializes under a different AppDomain (doesnt occur in IIS for me either)
Roman Nurik
Monday, January 23, 2006 11:05:12 AM (Central Standard Time, UTC-06:00)
Yes Roman, that is the problem.

I've tried a number of things to solve the problem - but without success.

The one idea I haven't tried is really out there... But you could use System.Diagnostics to get a list of all AppDomain objects in the process, and then maybe hook the assembly-not-found event of all those AppDomain objects so as to return the instance of your assembly. VERY much of a hack (if it would even work).
Monday, January 23, 2006 1:04:30 PM (Central Standard Time, UTC-06:00)
Yeah, I think that would be pushing the limits.. and since I can't register the temporary assembly App_Code.asdhv121 or what not to the GAC, I'll probably go along with your advice and run IIS instead of Crapsini

Thanks for the informative blog :)
Roman Nurik
Monday, January 30, 2006 11:30:23 PM (Central Standard Time, UTC-06:00)
I had the same thing happen, but unfortunately, the assembly containing the custom principal isn't strongly named (don't ask), so I can't put it in the GAC.

I was able to get away with putting it in the same directory as the ASP.NET Dev Server process (WebDev.WebServer.EXE), which happens to be the .NET framework runtime directory (e.g. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727). Painful, but at least it's easier to update than the GAC.
Scott Brady
Tuesday, January 31, 2006 7:25:05 AM (Central Standard Time, UTC-06:00)
Hi,
I'm sure I should be encountering this problem, but I don't seem to have (yet).
I have a custom principal defined in a separate assembly which is statically referenced by the site, and I use this with forms authentication in my site. Is there any way to force the appdomain switch to test ?
Tuesday, February 07, 2006 4:37:22 AM (Central Standard Time, UTC-06:00)
inoodle,
I'm experiencing this accessing the trace. Try turn tracing on, then access .../trace.axd or output the trace in the page after the principal is set - raises "SerializationException: Type is not resolved for member...". Will test now whether putting it in the GAC helps.
Thanks for the info above.
Andrew Venmore
Friday, February 10, 2006 3:03:33 PM (Central Standard Time, UTC-06:00)
Ugh I ran into this problem just like everyone else...how lame of Microsoft to shiop with this behavior 'by design'!

Anyway, aside from venting I though I would share my solution to the problem, which does not involve any additional deployment headaches.

Basically I make my custom IIdentity implement ISerializable. The trick then is to tell the runtime that the type being serialized is a GenericIdentity. Thus when my custom identity is deserialized, the runtime sees it as a GenericIdentity, whose assembly can always be found since it is part of the framework. I just came up with this so I'm not yet sure if I will run into any more gotchas with this, but so far it works just fine.

Private Sub GetObjectData( _
ByVal info As SerializationInfo, _
ByVal context As StreamingContext _
) Implements ISerializable.GetObjectData

' State will be CrossAppDomain for serialization induced by WebDev.WebServer
If context.State = StreamingContextStates.CrossAppDomain Then

Dim generic As New GenericIdentity(Me.Name, Me.AuthenticationType)
info.SetType(generic.GetType())

Dim serializableMembers As MemberInfo()
Dim serializableValues As Object()
serializableMembers = FormatterServices.GetSerializableMembers(generic.GetType())
serializableValues = FormatterServices.GetObjectData(generic, serializableMembers)

For i As Integer = 0 To serializableMembers.Length - 1
info.AddValue(serializableMembers(i).Name, serializableValues(i))
Next
Else
' Perform 'real' serialization here if necessary
Throw New InvalidOperationException("Serialization not supported")
End If

End Sub
John Lewicki
Monday, February 27, 2006 11:37:05 AM (Central Standard Time, UTC-06:00)
Me too.

Isn't it strange that u can't find more documentation on this? I mean, everyone who uses the Vs webdev-server and puts something homemade on the current thread (without also putting it in the GAC) should be having the same problem. I suppose that most asp.net-developers are using the webdev-server, and custom principals and identities should be quite common too. Yet, if u google "SerializationException: Type is not resolved for member" u won't get more than 10-15 hits or so, and most of em are irrelevant to our case.
So why aren't more people reporting and asking about this problem? Strange it is.

I'm gonna try a workaround like Daniel suggested above - using postbuild event to add my principal to the GAC.

/D
Big D
Tuesday, July 11, 2006 1:59:21 PM (Central Standard Time, UTC-06:00)
I did as John Lewicki suggested, except I had to implement ISerializable in both my custom identity and principal objects, and I am now able to host my web apps outside of IIS using WebDev.WebServer. Thanks!
nb
Friday, August 04, 2006 5:48:48 AM (Central Standard Time, UTC-06:00)
hey

I had just the same problem with my cassini, and your blog solved my problem. thanks a thousand!!!!
Martin og Petter
Wednesday, August 09, 2006 11:22:56 AM (Central Standard Time, UTC-06:00)
I'm still in the midst of investigating this, but it appears that making my custom identity derive from MarshalByRefObject also solves this problem. I'm not sure if it causes any perf problems on the IIS side.
Max M
Monday, August 21, 2006 7:04:18 AM (Central Standard Time, UTC-06:00)
The "Type is not resolved for member" might be hiding a different error.
I am using IIS and I experienced this error when using a third party dll that uses the CSLA. Strangely enough when I added the dlls to the GAC (thanks for this tip) I found that for some reason when Serializing my ArrayList it complained. So instead of returning an ArrayList, I now return an Array of the object. I just tested it now and I am able to remove the DLLs from the GAC and it works. So I think there might be a small chance that the error hides the real error during serialization. Anyway, thought I'll share that.
Anton Swanevelder
Monday, August 21, 2006 7:58:24 AM (Central Standard Time, UTC-06:00)
As to my post above, another error occured on serialization after I removed the DLLs from the GAC and BAM that stupid error is back. I bet if I fix the coding error the "Type is not resolved for member" error will not appear.
I am sure if I were able to debug the CSLA objects they would be hiding the error that is the real cause, could also be the third party control. Anyway, quite annoying.
Anton Swanevelder
Wednesday, August 30, 2006 5:21:44 AM (Central Standard Time, UTC-06:00)
I had same problems when trying to provide GenericPrincipal with a custom identity class.

However, when I created a custom principal class which implemented IPrinciapl and a custom identity class (implementing IIdentity), I never saw this exception anymore.

I guess, the problem is that GenericPrincipal for some reason tries to serialize identity object and fails if ISerializable is not implemented in identity class (like John posted above).
Wednesday, October 18, 2006 1:13:05 AM (Central Standard Time, UTC-06:00)
One way to get around the problem on the development machine without adding the assemblys to the GAC is to use the DEVPATH environment variable to instruct the .NET framework where to locate the assemblies.

For more information see:
http://blogs.msdn.com/junfeng/archive/2005/12/13/503059.aspx
http://msdn2.microsoft.com/en-us/library/cskzh7h6.aspx
Patrik Johansson
Tuesday, December 05, 2006 11:59:31 PM (Central Standard Time, UTC-06:00)
error message is "SerializationException: Type is not resolved for member..."

I had the same problem when attaching a custom principal to the thread.

It would seem that the VS2005 WebDev.WebServer can't resolve the TYPE of the custom principal, however when the code is deployed into a virtual directory, IIS has no problem resolving this custom principal.

Which is fine for deployment, but how do i debug.
(thanks to the guys above with the solution for the help on this one)

a) strong name the custom principal's dll (and dependancies)and register with the
GlobalAssemblyCache

OR

b) copy the custom principal's dll (and dependancies)to the framework2 folder
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

OR

c) install the "VS 2005 Web Application Project support"
http://msdn2.microsoft.com/en-us/asp.net/aa336618.aspx
(this uses the same project model as vs2003 and is great for migration)

Once installed, migrate your 2003 apps,
Right-click your "web application" click properties
(if you only see "property pages" you have a "vs2005 web site" not a "web app"
refer below to convert these)
Click the web tab
Scroll down and select "Use IIS Web server"
and click "Create Virtual Directory"

You will now debug IIS as opposed to VSWebDev and will resolve your custom
principal.


Hope you guys dont spend as much time on this as i have

Sean
sldroseAThotmailDOTcom



For those of you who have already migrated your old sites to 2005, you
can migrate a "vs2005 web site" to a "vs2005 web application" at this following link
http://webproject.scottgu.com/CSharp/Migration2/Migration2.aspx


Sean D
Wednesday, January 10, 2007 4:01:15 PM (Central Standard Time, UTC-06:00)
Thanks so much for your article! I encountered the same issue on a project recently, and it took a lot of research before I got to the root of the problem and found this page. The explaination you provided is great and makes perfect sense. Thanks again!
Joe Harker
Comments are closed.