Question

I was trying to create a remotable object and getting errors even though it seemed like everything was in order, so I decided to simplify and run a sample piece of code straight from Microsoft (http://msdn.microsoft.com/en-us/library/system.marshalbyrefobject.aspx).

When I run this code I get a TypeLoadException when it calls CreateInstanceAndUnwrap(). However, I just tried it on my laptop and it worked fine. Anybody have any clue why it would work on the laptop but not the desktop?? I just created a console app on each and copy/pasted the code below so I don't understand what would be different between the two.

using System;
using System.Reflection;

public class Worker : MarshalByRefObject
{
    public void PrintDomain() 
    { 
        Console.WriteLine("Object is executing in AppDomain \"{0}\"",
            AppDomain.CurrentDomain.FriendlyName); 
    }
}

class Example
{
    public static void Main()
    {
        // Create an ordinary instance in the current AppDomain
        Worker localWorker = new Worker();
        localWorker.PrintDomain();

        // Create a new application domain, create an instance 
        // of Worker in the application domain, and execute code 
        // there.
        AppDomain ad = AppDomain.CreateDomain("New domain");
        Worker remoteWorker = (Worker) ad.CreateInstanceAndUnwrap(
            Assembly.GetExecutingAssembly().FullName,
            "Worker");
        remoteWorker.PrintDomain();
    }
}

EDIT: So, I just tried to create an instance of the type using the Activator in the local domain and even that gave me an error saying that it couldn't load file or assembly 'System.RuntimeType' or one of its dependencies. Seeing as how that's an internal type to the .Net Framework that I couldn't keep it from deploying even if I wanted to, I'm quite baffled now.

EDIT: There is no InnerException, but here's the Stack:

   at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type)
   at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
   at System.Activator.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(String assemblyName, String typeName)
   at System.AppDomain.CreateInstance(String assemblyName, String typeName)
   at System.AppDomain.CreateInstance(String assemblyName, String typeName)
   at ConsoleApplication1.Example.Main(String[] args) in D:\Projects\Remoting\ConsoleApplication1\Program.cs:line 28
   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()

Oh, and the exception reads:

{"Could not load type 'Worker' from assembly 'ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.":"Worker"}

Also, I tried running this line of code:

var w = Activator.CreateInstance(typeof(Worker).GetType().FullName, "Worker");

and got a FileNotFoundException:

Could not load file or assembly 'System.RuntimeType' or one of its dependencies.  The system cannot find the file specified.
Was it helpful?

Solution

In your initial code, you are trying to instantiate an incomplete typename, and that code shouldn't work anywhere unless Worker has no namespace. It should be:

var workerType = typeof(Worker);
Worker remoteWorker = (Worker) ad.CreateInstanceAndUnwrap(
            // Where Worker is defined (probably the same as current assembly in you case)
            workerType.Assembly.FullName,
            // Worker's *fully-qualified* typename, including the namespace 
            workerType.FullName); 

Your local activation code is mistakenly calling typeof(Worker).GetType().FullName rather than typeof(Worker).FullName.

typeof(Anything).GetType() will always return System.RuntimeType

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top