473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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(AssemblyTy pe, 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
Nov 16 '05 #1
1 3698
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(FooDel egate), 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
Ajay Pal Singh wrote:
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(AssemblyTy pe,
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

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Russ McDaniel | last post by:
Originally posted to microsoft.public.dotnet.distributed_apps with no response. Reposted here with additional thoughts. --- Hello, I'm writing a Windows service which performs some...
3
by: Amjad | last post by:
Hi, I just wrote a test Windows Service that creates a text file on startup (please see my code below). The file is never created. Protected Overrides Sub OnStart(ByVal args() As String) Dim...
4
by: Blaxer | last post by:
I have read approximately 30 articles now on various methods to make your vb.net application automatically update itself and I can't see how they apply to a vb.net windows services projects. The...
10
by: John | last post by:
I currently have a Windows Service that runs Transactions that are very Processor/Memory Intensive. I have a requirement to deploy multiple instances of the Web service on the Same server. Each...
17
by: UJ | last post by:
Is there any way for a windows service to start a windows program ? I have a service that will need to restart a windows app if it needs to. TIA - Jeff.
2
by: deko | last post by:
When to use a privileged user thread rather than a windows service? That's the question raised in a previous post . It was suggested that if the service needs to interact with a WinForms app...
4
by: cmgarcia17 | last post by:
I have two windows services that I've written: the first is a "Listener Service" that listens for MSMQ Message delivery. The second is a "Watcher Service" that monitors the state of the first. If...
12
by: Anil Gupte | last post by:
I wrote my Windows Service first as a regular Windows Exe because it is easier to debug. In that I used AppDir = Application.ExecutablePath.Substring(0,...
5
by: dm3281 | last post by:
I'm really starting to hate writing services -- or trying to, anyway. Why do I need to rename my project to the service name? Why do I need to set the "ServiceName" property to my service name?...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.