Performance when dynamically loading assembly to do plugins in Web Services

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Benjamin

    Performance when dynamically loading assembly to do plugins in Web Services

    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 LoadDatabasePlu gin( string assemblyFile, string
    objectType )
    {
    IDatabase dbLogic = null;
    Assembly assembly;
    Type type;

    try
    {
    // Try loading the specified assembly.
    assembly = Assembly.LoadFr om(assemblyFile );
    if (assembly != null)
    {
    // Get the object type we want.
    type = assembly.GetTyp e( objectType );
    if( type != null )
    {
    // Create an instance of the object.
    dbLogic = (IDatabase)Acti vator.CreateIns tance(type);
    }
    }
    }
    catch
    {
    return null;
    }
    return dbLogic;
    }

    where "assemblyFi le" is the assembly file ("d:\dbmsaccess .dll") and
    "objectType " is the class I want to create
    ("MyProject.Dat abase.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.LoadFr om() 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
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Performance when dynamically loading assembly to do plugins in Web Services

    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.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Benjamin" <benjaminberube @hotmail.com> wrote in message
    news:3f0886f0.0 409300735.67490 92f@posting.goo gle.com...[color=blue]
    > 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 LoadDatabasePlu gin( string assemblyFile, string
    > objectType )
    > {
    > IDatabase dbLogic = null;
    > Assembly assembly;
    > Type type;
    >
    > try
    > {
    > // Try loading the specified assembly.
    > assembly = Assembly.LoadFr om(assemblyFile );
    > if (assembly != null)
    > {
    > // Get the object type we want.
    > type = assembly.GetTyp e( objectType );
    > if( type != null )
    > {
    > // Create an instance of the object.
    > dbLogic = (IDatabase)Acti vator.CreateIns tance(type);
    > }
    > }
    > }
    > catch
    > {
    > return null;
    > }
    > return dbLogic;
    > }
    >
    > where "assemblyFi le" is the assembly file ("d:\dbmsaccess .dll") and
    > "objectType " is the class I want to create
    > ("MyProject.Dat abase.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.LoadFr om() 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[/color]


    Comment

    Working...