473,714 Members | 2,485 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 InvokeMethodOfA notherAssembly( )
{
string AssemblyName = @"D:\SchedularS ervice\TesterAp plication\bin\D ebug\TesterAppl ication.dll";
string ClassName = "TesterClas s";
string MethodName = "TesterMeth od";
Assembly AnotherAssembly
Type AssemblyType;
MethodInfo MethodInfo;
Thread thread;
ThreadStart threadDelegate;

AnotherAssembly = Assembly.LoadFr om(AssemblyName );
AssemblyType = AnotherAssembly .GetType(Anothe rAssembly.GetNa me().Name + "." + ClassName, true, true);
MethodInfo = AssemblyType.Ge tMethod(MethodN ame);

threadDelegate = (ThreadStart)Th readStart.Creat eDelegate(Assem blyType, MethodInfo);
thread = new Thread(threadDe legate);
thread.Start();
}
When i run the above code I get following exception:

"An unhandled exception of type 'System.Argumen tException' 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 3739
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.Create Delegate() 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.Create Delegate() 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(str ing str, int i);

public void InvokeMethodOfA notherAssembly( )
{
<snip>
AnotherAssembly = Assembly.LoadFr om(AssemblyName );
AssemblyType = AnotherAssembly .GetType(...);
MethodInfo = AssemblyType.Ge tMethod(MethodN ame);

FooDelegate fooDel =
(FooDelegate)De legate.CreateDe legate(typeof(F ooDelegate), MethodInfo);
fooDel.BeginInv oke("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 InvokeMethodOfA notherAssembly( )
{
string AssemblyName =
@"D:\SchedularS ervice\TesterAp plication\bin\D ebug\TesterAppl ication.dll";
string ClassName = "TesterClas s";
string MethodName = "TesterMeth od";
Assembly AnotherAssembly
Type AssemblyType;
MethodInfo MethodInfo;
Thread thread;
ThreadStart threadDelegate;

AnotherAssembly = Assembly.LoadFr om(AssemblyName );
AssemblyType = AnotherAssembly .GetType(Anothe rAssembly.GetNa me().Name +
"." + ClassName, true, true);
MethodInfo = AssemblyType.Ge tMethod(MethodN ame);

threadDelegate = (ThreadStart)Th readStart.Creat eDelegate(Assem blyType,
MethodInfo);
thread = new Thread(threadDe legate);
thread.Start();
}
When i run the above code I get following exception:

"An unhandled exception of type 'System.Argumen tException' 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
2767
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 potentially long-running, uninterruptable processes. When the service starts, I create a separate worker thread to perform the work so as to not block the service control manager upon startup. The design quandry I'm running into though is how best
3
14936
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 swLog As StreamWriter = File.CreateText("C:\myLog.txt") swLog.WriteLine("My Windows Service has just started.") swLog.Close() : swLog.Flush() End Sub
4
5601
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 goal of the project is to write a windows service in vb.net that runs on many workstations that can automatically update itself from a central web server or share (either or doesn’t matter). I do not want the service to re-install itself each time...
10
4978
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 Instance needs to run in its own process. My current approach to this is to put all the logic into a separate "Worker" assembly and install it into the GAC. I'm then going to create Multiple Windows Services (i.e. MyService1, MyService2 etc..)...
17
6438
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
6893
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 (which is the UI used to adjust the actions taken by, and the schedule of the service), then a privileged user thread should be used in the UI - no service required. But... "A windows service enables the creation of long-running executable
4
6452
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 the "Listener Service" goes down I want the "Watcher Service" to receive a notification and perform a set action. To enable this communication I've written a separate assembly (that both services reference) that exposes two interfaces :...
12
3710
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, Application.ExecutablePath.LastIndexOf("\")) ' Where this application Excutable sits Now, when I am converting it to a Service, what path can I use? Or can I specify a path in the service to always use as a data path? I would prefer the former because I do not know the layout of...
5
3304
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? Why do I need to set a property within my code to the service name? Are all these required or am I just doing this for consistency purposes?
0
8803
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9314
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6636
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5948
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2112
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.