Can't use AppDomains inside WCF called methods - by Steve Hansen [2sky]

Status : 

  By Design<br /><br />
		The product team believes this item works according to its intended design.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 712500 Comments
Status Closed Workarounds
Type Bug Repros 1
Opened 12/8/2011 7:21:04 AM
Access Restriction Public


Using a new AppDomain inside a method that is called as a WCF service gives the following exception:

"Type 'System.Reflection.Emit.AssemblyBuilder' in assembly 'mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable."

Which is triggered by the following code:
        public void DoWork()
            var appDomain = AppDomain.CreateDomain("Test");
            var asmBuilder = appDomain.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.Run);

I tried on the following environments:
- .NET 4.0: Client/Full
- .NET 4.5: Client/Full
- x86/x64
- Hosted on ServiceHost, IIS or Azure

All give the exact same exception.
Sign in to post a comment.
Posted by Microsoft on 12/19/2011 at 2:08 PM
Thanks Steve,
I took a look at the repro and the documentation for the overload method for DefineDynamicAssembly ( says that this overload should only be called from the current app domain. Once I did that, there was no exception.
This page also notes the recommended way if you are creating your own app domain -
"During the development of code that emits dynamic assemblies, it is recommended that you use an overload of the DefineDynamicAssembly method that specifies evidence and permissions, supply the evidence you want the dynamic assembly to have, and include SecurityPermissionFlag.SkipVerification in refusedPermissions. Including SkipVerification in the refusedPermissions parameter ensures that the MSIL is verified. A limitation of this technique is that it also causes SecurityException to be thrown when used with code that demands full trust."
I am resolving this as By Design. Please reactivate/ create a new bug if you are still having any issues.
Posted by MS-Moderator08 [Feedback Moderator] on 12/8/2011 at 8:47 PM
Thank you for submitting feedback on Visual Studio 2010 and .NET Framework. Your issue has been routed to the appropriate VS development team for investigation. We will contact you if we require any additional information.
Posted by MS-Moderator01 on 12/8/2011 at 8:44 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(