Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass an unknown type between two .NET AppDomains?
    primarykey
    data
    text
    <p>I have a .NET application in which assemblies in separate AppDomains must share serialized objects that are passed by value.</p> <p>Both assemblies reference a shared assembly that defines the base class for the server class and also defines the base class for the entiy type that will be passed between domains:</p> <pre><code>public abstract class ServerBase : MarshalByRefObject { public abstract EntityBase GetEntity(); } [Serializable] public abstract class EntityBase { } </code></pre> <p>The server assembly defines the server class and a concrete implemetation of the entity type:</p> <pre><code>public class Server : ServerBase { public override EntityBase GetEntity() { return new EntityItem(); } } [Serializable] public class EntityItem : EntityBase { } </code></pre> <p>The client assembly creates the <code>AppDomain</code> in which the server assembly will be hosted and uses an instance of the server class to request a concrete instance of the entity type:</p> <pre><code>class Program { static void Main() { var domain = AppDomain.CreateDomain("Server"); var server = (ServerBase)Activator.CreateInstanceFrom( domain, @"..\..\..\Server\bin\Debug\Server.dll", "Server.Server").Unwrap(); var entity = server.GetEntity(); } } </code></pre> <p>Unfortnately, this approach fails with a <code>SerializationException</code> because the client assembly has no direct knowledge of the concrete type that is being returned.</p> <p>I have read that .NET remoting supports unknown types when using binary serialization, but I am not sure whether this applies to my setup or how to configure it.</p> <p>Alternatively, is there any other way of passing an unknown concrete type from the server to the client, given that the client only needs to access it via its known base class interface.</p> <p>Thanks for your advice,</p> <p>Tim</p> <p>EDIT:</p> <p>As requested by Hans, here is the exception message and stack trace.</p> <pre><code>SerializationException Type is not resolved for member 'Server.EntityItem,Server, Version=1.0.0.0,Culture=neutral, PublicKeyToken=null'. at Interop.ServerBase.GetEntity() at Client.Program.Main() in C:\Users\Tim\Visual Studio .Net\Solutions\MEF Testbed\Client\Program.cs:line 12 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload