开发者

Load A Assembly in Runtime and call a Method and unload the assembly

开发者 https://www.devze.com 2023-03-15 03:54 出处:网络
Im creating an application, wich will conect to several sql database and get some details form the database,

Im creating an application, wich will conect to several sql database and get some details form the database,

In this application i have to encrypt the database connection details such as user name passwords. yes its pritty straight forward and simple just write a metod to decrypt the credentials.

but in my case i have to rely on third party encription mechanisam to decrypt the credentials. more over i have to connect to several sql servers which will again used some other encryption methods. hence im cording my application to load a encryption assembly dynamically and call the encryption method.

but when i load the assembly form Assembly.LoadFile("Path") i cannot unload the loaded assembly. i think i have load this assembly in separate app domain and call the relavant methods and unload that appdomain. im needing some help on this part. due to my lack of knoladge i cannot call the required method. my code as follows. please help me on this.

class 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";

    }



}

class 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();
    }


}

Thanks in advance.


I have figured out how to do this and hope fully it will be successful please find the below code which if used to over come the situation

       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();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消