Dynamically load assembly from config file

N

Nelson Xu

Hi All,
I am wondering how can I dynamically load a .NET assembly
from config file in my Windows Service.

Thank you in advance !

Nelson
 
M

Matt Berther

Hello Nelson,

If you mean an assembly, look at the Assembly.LoadFrom method.

If you really mean a type, look at Type.GetType(string) and Activator.CreateInstance(type).

Both are well documented in the MSDN.
 
N

Nelson Xu

Hi Matt,
Thank you for your response.
Here is what I would like to do:
<1> Create a Windows Service;
<2> Run some task every two hours or whatever interval
value setting in config file;
<3> In order to run this task, I need to create an instane
of some dll(assembly);
<4> Instead of adding a reference to this dll, I prefer to
load it from config file.

Please advise.

Nelson
-----Original Message-----
Hello Nelson,

If you mean an assembly, look at the Assembly.LoadFrom method.

If you really mean a type, look at Type.GetType(string)
and Activator.CreateInstance(type).
 
M

Matt Berther

Hello Nelson,

Then you probably also want to look that the ConfigurationSettings class
so that you can leverage the .NET Configuration API.

Create the configuration file, and then derive your custom objects from a
common interface, so that you can call an appropriate method to execute the
task.

For example:

public interface ITask
{
void Execute();
}

public class MySampleTask : ITask
{
public void Execute()
{
// do something in here
}
}

In your configuration file, put the type information for MySampleTask and
then use Activator.CreateInstance to load it.

Example:

Type type = Type.GetType(ConfigurationSettings.AppSettings["taskType"], true);
ITask task = Activator.CreateInstance(type) as ITask;
if (task != null)
{
task.Execute();
}
else
{
// something happened and the type couldnt be loaded
}

The type will need to be referenced by a fully qualified name (ie: namespace
and assembly) in the config file.

Hope this helps!
 
S

Shariq Khan

Also:

here is an article on how to write Windows' services:
http://msdn.microsoft.com/msdnmag/issues/01/12/NETServ/




Matt Berther said:
Hello Nelson,

Then you probably also want to look that the ConfigurationSettings class
so that you can leverage the .NET Configuration API.

Create the configuration file, and then derive your custom objects from a
common interface, so that you can call an appropriate method to execute
the task.

For example:

public interface ITask
{
void Execute();
}

public class MySampleTask : ITask
{
public void Execute()
{
// do something in here
}
}

In your configuration file, put the type information for MySampleTask and
then use Activator.CreateInstance to load it.

Example:

Type type = Type.GetType(ConfigurationSettings.AppSettings["taskType"],
true);
ITask task = Activator.CreateInstance(type) as ITask;
if (task != null)
{
task.Execute();
}
else
{
// something happened and the type couldnt be loaded
}

The type will need to be referenced by a fully qualified name (ie:
namespace and assembly) in the config file.

Hope this helps!

--
Matt Berther
http://www.mattberther.com
Hi Matt,
Thank you for your response.
Here is what I would like to do:
<1> Create a Windows Service;
<2> Run some task every two hours or whatever interval
value setting in config file;
<3> In order to run this task, I need to create an instane
of some dll(assembly);
<4> Instead of adding a reference to this dll, I prefer to
load it from config file.
Please advise.

Nelson

and Activator.CreateInstance(type).
 
N

Nelson Xu

Hi Matt,
Thank you so much for your response.
Can you send me a code example to show what exactly looks like in config
file for type and assembly because I am kind of confusing between term
type and assembly.

Thanks again !

Nelson
 
N

Nelson Xu

Hi Shariq,
Thank you so much for your response.
Could you please send me an code sample to dynamically load assembly
from config(web.config or machine.config) file if you have one.

Best Regards,

Nelson
 

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