在应用程序中加载dll时在web.config中生成连接字符串 [英] Generating connection string in web.config while loading dll in application

查看:49
本文介绍了在应用程序中加载dll时在web.config中生成连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个类库文件并在web应用程序中使用了这个文件的dll



i在这个dll中编写了一些代码,比如生成连接字符串以及appSettings在web.config文件中



但问题是....我想生成这个连接字符串和appsettings数据,同时在Web应用程序中添加此dll而不是运行申请



所以PLZ帮帮我...



有些frnz不明白我的问题
让我清楚..



当我在我的应用程序中添加dll时它成功添加

没问题。

当我运行我的应用程序然后在web.config中生成连接字符串



bt这会发生什么...

所有会话都被清除



因此我想在应用程序中添加此dll时生成此连接字符串



所以有任何事件处理程序或加载器可用,这将有助于我得到结果

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

推荐答案

如果你想在运行时添加一个DLL并使用它的方法,那么你必须使用已经可用的动态加载技术在C#中。

命名空间:

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:
using System.Reflection;
using System.IO; // For FileNotFoundException definition.



code :


code:

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..


这篇关于在应用程序中加载dll时在web.config中生成连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆