Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a class library file and use the dll of this file in web application

i write some code in this dll like generating connection string as well as appSettings in web.config file

but problem is that.... i want to generate this connection string and appsettings data while adding this dll in web application instead of running the application

so plz help me...

some frnz are not understand my question
let me clear..

when i add dll in my application it added successfully
no problem about it.
when i run my application then connection string generate in web.config

bt what happen with this...
all sessions are cleared

hence i want to generate this connection string at the time of adding this dll in application

so is there any event handler or loader is available which will help to get me result
Posted
Updated 29-Mar-13 22:49pm
v3
Comments
[no name] 29-Mar-13 9:21am    
Huh? What? That makes no sense at all. Why would you need to generate connection data when the application is not running at all?
Abhinav S 29-Mar-13 9:54am    
Do you mean you want to generate the connection string dynamically?
Shree.grace 30-Mar-13 1:05am    
i think you are not understand my question..
let me clear...

when i add dll in my application it added successfully
no problem about it.
when i run my application then connection string generate in web.config

bt what happen with this...
all sessions are cleared

hence i want to generate this connection string at the time of adding this dll in application

so is there any event handler or loader is available which will help to get me result
Raza_theraaz 30-Mar-13 1:53am    
Do you want to load a dll at runtime and use its methods?
Shree.grace 30-Mar-13 2:17am    
ya..
but this method will execute at once when i add this dll in apllication
instead of executing this method every time when i run the application

1 solution

If you want to add a dll at run time and use its methods, then you have to use dynamic loading technique already available in C#.
Namespaces:
C#
using System.Reflection;
using System.IO; // For FileNotFoundException definition.

code:
C#
public void myDllStuff(string myDll) // Put your dll/assembly here
{
string dllName = myDll; 
Assemly asm = null;

// Try to load assembly
 try{
     asm = Assembly.Load(asmName); //you can use Assembly.LoadFrom for more flexibility 
    Type[] types = asm.GetTypes(); // To get types from your dll
    noArgumentMethod(asm, "myDll.myMethod");// put your method and dll
    withArgumentMethod(asm, "myDll.myArgumentMethod", true, 1);
    }
 catch
    {
     // Can't find assembly. 
     // Put your exception code.
    }
}
public void noArgumentMethod(string asm, string methodName)
{
// below is the code to run a method from your dll which have no arguments.
    Type myMethod = asm.GetType(methodName); 
    object obj = Activator.CreateInstance(myMethod);
    MethodInfo mi = myMethod.GetMethod(methodName); //"myMethod"
    mi.Invoke(obj, null);
// the above line will run the method and perform whatsoever is within the method of dll. 
}

public void withArgumentMetho(string asm, string methodName, bool argument1, int argument2)
{
  // assumning two arguments
 Type myArgumentMethod = asm.GetType(methodName);//"myDll.myArgumentMethod"
 object obj = Activator.CreateInstance(myArgumentMethod);
// Invoke myArgumentMethod() with two arguments
 MethodInfo mi = myArgumentMethod("myArgumentMethod");
 mi.Invoke(obj, new object[]{argument1, argument2});
}


You said that you want this whole process to run only for one time, you can do this by setting a flag or saving to a file or database on server.
Hope it helps..
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900