Performance when dynamically loading assembly to do plugins in Web Services

  • Thread starter Thread starter Benjamin
  • Start date Start date
B

Benjamin

Hi,

I'm currently writing a Web Services that interacts with a database.
To allow me to use not just one database provider (for example, I
could use MS Access, SQL Server or MySQL), the Web Service dynamically
loads up an assembly that implements an Interface I called IDatabase.

To load the assembly and create an object, I wrote this function:

private IDatabase LoadDatabasePlugin( string assemblyFile, string
objectType )
{
IDatabase dbLogic = null;
Assembly assembly;
Type type;

try
{
// Try loading the specified assembly.
assembly = Assembly.LoadFrom(assemblyFile);
if (assembly != null)
{
// Get the object type we want.
type = assembly.GetType( objectType );
if( type != null )
{
// Create an instance of the object.
dbLogic = (IDatabase)Activator.CreateInstance(type);
}
}
}
catch
{
return null;
}
return dbLogic;
}

where "assemblyFile" is the assembly file ("d:\dbmsaccess.dll") and
"objectType" is the class I want to create
("MyProject.Database.Logic").

This works fine, when I change the DLL file name, the new assembly
code get loaded and I can which from MSAccess to MSSQLserver without
problem and without recompiling my Web Service.

My concerns is this: what performance problems can I have as I get
thousands of simultanious users accessing my Web Service? Does
calling Assembly.LoadFrom() actually loads the file on each call? Or
does .Net checks if it's not already loaded? I'm trying to find a way
to do a check before calling LoadFrom() in case the DLL is already
loaded.

Please, tell me if this whole approach of dynamically loading the
assembly is OK, and how I can improve it so that I don't get slowdowns
as more users query the Web Service.

Thanks for your time,

Benjamin
 
Benjamin,

You shouldn't have to do this. The assembly can only be loaded once,
and the Assembly will not be loaded a second time. The assembly will be
loaded until the app-domain exits.

The code you have should be fine.

Hope this helps.
 
Back
Top