在运行时加载程序集并调用方法并卸载程序集

如何解决在运行时加载程序集并调用方法并卸载程序集

|| 在创建应用程序时,它会连接到多个sql数据库并从数据库中获取一些详细信息, 在此应用程序中,我必须加密数据库连接详细信息,例如用户名密码。是的,它的简单明了而简单,只需编写一个方法来解密凭据即可。 但就我而言,我必须依靠第三方加密机制来解密凭据。更重要的是,我必须连接到多个sql服务器,这将再次使用其他一些加密方法。因此,我的应用程序被迫动态加载加密程序集并调用加密方法。 但是,当我从Assembly.LoadFile(\“ Path \”)加载程序集窗体时,无法卸载已加载的程序集。我想我已经在单独的应用程序域中加载了该程序集,并调用了相关方法并卸载了该应用程序域。我在这方面需要一些帮助。由于我缺乏知识,我无法调用所需的方法。我的代码如下。请帮助我。 类ApplicationSettings     {
    private static ApplicationSettings m_ApplicationSettings;
    public String m_ServerName { get; private set; }
    public String m_DatabaseName { get; private set; }
    public String m_UserID { get; private set; }
    public String m_Password { get; private set; }
    public String m_EncryptionDLLPath{ get; private set; }
    public String m_NameSpace { get; private set; }
    public String m_ClassName { get; private set; }
    public String m_EncryptionMethodName { get; private set; }
    public String m_DecryptionMethodName { get; private set; }

    private ApplicationSettings()
    {
        m_ApplicationSettings = this;
    }

    public static ApplicationSettings CurrentValues
    {
        get
        {                
            return m_ApplicationSettings;
        }
        private set
        {
            m_ApplicationSettings = value;
        }
    }

    internal static void Initialize()
    {
        CommonFunctions.DataEncryption _enc = new CommonFunctions.DataEncryption();


        ApplicationSettings.CurrentValues = new ApplicationSettings();
        ApplicationSettings.CurrentValues.m_EncryptionDLLPath = @\"C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\Projects\\TestApp\\TestApp\\bin\\Debug\\AppSec.dll\";
        ApplicationSettings.CurrentValues.m_NameSpace = \"AppSec\";
        ApplicationSettings.CurrentValues.m_ClassName = \"AppSecEncDec\";
        ApplicationSettings.CurrentValues.m_EncryptionMethodName = \"Encrypt\";
        ApplicationSettings.CurrentValues.m_DecryptionMethodName = \"Decrypt\";
        ApplicationSettings.CurrentValues.m_Password = _enc.Decrypt(\"pzBS3EJDoGM=\");
        ApplicationSettings.CurrentValues.m_UserID = \"sa\";

    }



}
类DataEncryption     {
    AppDomain DomainName;        

    //Call the Encryption Method 
    public String Encrypt(Object _DataToEncrypt)
    {


    }

    //Call the Decryption Method 
    public String Decrypt(Object _DataToDecrypt)
    {
        String _Decrypt = \"\";

        String assemblyFileName = ApplicationSettings.CurrentValues.m_EncryptionDLLPath;
        String assemblyName = ApplicationSettings.CurrentValues.m_NameSpace;

        //Setup the evidence
        Evidence evidence = new Evidence(AppDomain.CurrentDomain.Evidence);
        AppDomain TestDomain = AppDomain.CreateDomain(
          \"TestDomain\",//The friendly name of the domain.
          evidence,//Evidence mapped through the security policy to establish a top-of-stack permission set.
          AppDomain.CurrentDomain.BaseDirectory,// The base directory that the assembly resolver uses to probe for assemblies.
          System.IO.Path.GetFullPath(assemblyFileName),// The path relative to the base directory where the assembly resolver should probe for private assemblies.
          true  // If true,a shadow copy of an assembly is loaded into this application domain.
          );
        string s = TestDomain.Load(assemblyName).FullName;
        string[] myparam = new String[1];
        myparam[0] = \"test\";


        TestDomain.CreateInstance(TestDomain.Load(assemblyName).GetName().ToString(),ApplicationSettings.CurrentValues.m_NameSpace + \".\" + ApplicationSettings.CurrentValues.m_ClassName).CreateObjRef(GetType());
        //her i need to execute the Encrypt method which will load form the third party encryption mechanisam

        //method name will be returnd on this parameter in application settings Classes.ApplicationSettings.CurrentValues.m_EncryptionMethodName ;

        UloadAssembly();

        return _Decrypt;
    }


    public void UloadAssembly()
    {
        //Unload the loaded appdomain
        AppDomain.Unload(DomainName);            
        GC.Collect();
    }


}
提前致谢。     

解决方法

        我已经弄清楚如何做到这一点,并希望它能够成功,请找到下面的代码,如果用来克服这种情况
       public String Encrypt(Object _DataToEncrypt)
    {
        try
        {

            String _Encrypt = \"\";
            LoadAssembly();
            ShowLoadedAssemblies();
            if (ClassInstance != null)
            {
                MethodInfo EncryptionMethod = ClassInstance.GetType().GetMethod(Classes.ApplicationSettings.CurrentValues.m_EncryptionMethodName); ;
                if (EncryptionMethod != null)
                {
                    object[] myparam = new object[1];
                    myparam[0] = _DataToEncrypt;
                    _Encrypt = (string)EncryptionMethod.Invoke(null,myparam);
                }

            }

            UloadAssembly();
            ShowLoadedAssemblies();
            return _Encrypt;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);


        }

    }

    //Call the Decryption Method 
    public String Decrypt(Object _DataToDecrypt)
    {
        String _Decrypt = \"\";

        LoadAssembly();
        ShowLoadedAssemblies();
        if (ClassInstance != null)
        {
            MethodInfo DecryptionMethod = ClassInstance.GetType().GetMethod(Classes.ApplicationSettings.CurrentValues.m_DecryptionMethodName);;
            if (DecryptionMethod != null)
            {
                object[] myparam = new object[1];
                myparam[0] = _DataToDecrypt;
                _Decrypt = (string)DecryptionMethod.Invoke(null,myparam);
            }               

        }
        UloadAssembly();
        ShowLoadedAssemblies();
        return _Decrypt;
    }
    //Loading the Assembly 
    public void LoadAssembly()
    {




        Evidence evi = new Evidence(AppDomain.CurrentDomain.Evidence);

        DomainName = AppDomain.CreateDomain(Classes.ApplicationSettings.CurrentValues.m_NameSpace,evi,AppDomain.CurrentDomain.BaseDirectory,Classes.ApplicationSettings.CurrentValues.m_EncryptionDLLPath,true
                                            );

       String LoadingAssemblyName = AssemblyName.GetAssemblyName(Classes.ApplicationSettings.CurrentValues.m_EncryptionDLLPath).FullName;

       ClassInstance = DomainName.CreateInstanceAndUnwrap(LoadingAssemblyName,Classes.ApplicationSettings.CurrentValues.m_NameSpace 
                                                              + \".\" 
                                                              + Classes.ApplicationSettings.CurrentValues.m_ClassName
                                                           );

    }
    public void UloadAssembly()
    {
        //Unload the loaded appdomain
        AppDomain.Unload(DomainName);            
        GC.Collect();
    }
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?