2
votes

All,

I'm getting an unexplained error when calling CreateInstanceAndUnwrap on a new appdomain I have created. The full error text is:

Type 'System.Net.Http.HttpRequestMessage' in assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

The child appdomain is created within a web API app, and the type I'm attempting to create a proxy for derives from MarshalByRefObject.

The assembly where this type is defined has no reference to System.Net.Http or System.Web - so I'm not clear why HttpRequestMessage serializability has any bearing.

My guess is it's trying to copy some contextual data over from the parent AppDomain to the child one, but perhaps someone here could shed some light on what's going on?

Here's the relevant code:

var appDomain = AppDomain.CreateDomain("AName", null, AppDomain.CurrentDomain.SetupInformation);
SomeClient client = appDomain.CreateInstanceAndUnwrap(typeof(SomeClient).Assembly.FullName, typeof(SomeClient).FullName) as SomeClient;

The "SomeClient" type and its parent assembly don't have any web/system.net.http references.

1
please show us line code or even more lines it is hard to deduct without a wider context - profesor79
The code that uses HttpRequestMessage must strictly run in the AppDomain, you cannot expose it in your MBRO derived class through a public property or method argument. Ought to be pretty tricky in a web app And expensive. - Hans Passant
Thanks - sample code added. I'm not trying to marshal the HTTP request across the app domains, in fact that's what I'm trying to avoid. - booler

1 Answers

-1
votes

I found the answer.

The problem was in our own custom logging framework, which is hooked into the application via a delegating handler.

In this logging framework, another developer was manipulating the call context: https://msdn.microsoft.com/library/w61s16a1(v=vs.100).aspx

storing the entire HTTP Request in it. When spinning up the new AppDomain, it was attempting to flow this call context over, hence the serialization error.