Through a Windows service running a method of an assembly in a separate process

A

Ajay Pal Singh

Hi,

I am making an windows service similar to windows task schedular. The service would invoke the methods of some assemblies at some predefined schedules. (windows service read the method/assembly and schedule info from app.config file)
I am using reflection to dynamically load the assemblies and then want to call a method of that assembly on separate thread. I am using code similar to following :

public void InvokeMethodOfAnotherAssembly()
{
string AssemblyName = @"D:\SchedularService\TesterApplication\bin\Debug\TesterApplication.dll";
string ClassName = "TesterClass";
string MethodName = "TesterMethod";
Assembly AnotherAssembly
Type AssemblyType;
MethodInfo MethodInfo;
Thread thread;
ThreadStart threadDelegate;

AnotherAssembly = Assembly.LoadFrom(AssemblyName);
AssemblyType = AnotherAssembly.GetType(AnotherAssembly.GetName().Name + "." + ClassName, true, true);
MethodInfo = AssemblyType.GetMethod(MethodName);

threadDelegate = (ThreadStart)ThreadStart.CreateDelegate(AssemblyType, MethodInfo);
thread = new Thread(threadDelegate);
thread.Start();
}
When i run the above code I get following exception:

"An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Type must derive from Delegate."

I can't understand why exception is occuring. Can anyone give me a clue or a workaround for the scenario.

Thanks,
Ajay
 
J

Joakim Karlsson

You have to have a delegate declared with the correct signature before
you can have an instance of that delegate created. That is, you must
know the signature of the method you want to call in the loaded assembly.

If you want to use the Delegate.CreateDelegate() method to create a
delegate for calling your dynamically loaded method, that method has to
be declared as static. Otherwise you will have to create an instance of
the type exposing the method, and create a delegate that wraps the
method you need before you can call it.

The Delegate.CreateDelegate() method takes the type of the *delegate*
that you want to create, not the type defining the (static) method you want.

Perhaps you could change your code to something like this:

public delegate void FooDelegate(string str, int i);

public void InvokeMethodOfAnotherAssembly()
{
<snip>
AnotherAssembly = Assembly.LoadFrom(AssemblyName);
AssemblyType = AnotherAssembly.GetType(...);
MethodInfo = AssemblyType.GetMethod(MethodName);

FooDelegate fooDel =
(FooDelegate)Delegate.CreateDelegate(typeof(FooDelegate), MethodInfo);
fooDel.BeginInvoke("hello", 1, null, null);
}

In order not to leak resources you should also make sure to call
EndInvoke at an appropriate time, so don't take the example above too
literarilly!!

/Joakim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top