zoukankan      html  css  js  c++  java
  • .net remoting accross App Domain

    下面这个例子介绍了如何利用.net remoting 来实现App Domain间的访问,而且通过创建一个新的App Domain来加载一些untrusted的dlls,保证了程序自身的App Domain的边界安全。
    下载
    下面是程序的main函数:
    namespace Microsoft.Samples.Application
    {
        
    static class Program
        {
            
    static void Main()
            {
                
    //Create new appDomain
                AppDomain domain = AppDomain.CreateDomain("NewAppDomain");

                
    // Create remote object in new appDomain via shared interface
                // to avoid loading the implementation library into this appDomain
                IHelloWorld proxy = 
                    (IHelloWorld)domain.CreateInstanceAndUnwrap(
                        
    "ImplementationLibrary"
                        
    "Microsoft.Samples.ImplementationLibrary.HelloWorld");

                
    // Output results of the call
                Console.WriteLine("\nReturn:\n\t{0}", proxy.Echo("Hello"));
                Console.WriteLine();

                Console.WriteLine(
    "Non-GAC assemblies loaded in {0} appDomain:", AppDomain.CurrentDomain.FriendlyName);
                
    foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    
    if (!assembly.GlobalAssemblyCache)
                    {
                        Console.WriteLine(
    "\t" + assembly.GetName().Name);
                    }
                }
                Console.WriteLine(
    "\nImplementationLibrary should not be loaded.");

                Console.ReadLine();
            }
        }
    }

    下面是被load进新创建的Domaindll

    namespace Microsoft.Samples.ImplementationLibrary
    {
        
    internal class HelloWorld : MarshalByRefObject, IHelloWorld
        {

            
    #region IHelloWorld Members

            
    string IHelloWorld.Echo(string input)
            {
                ConsoleColor originalColor = Console.BackgroundColor;
                Console.BackgroundColor = ConsoleColor.Blue;

                
    string currentAppDomainName = AppDomain.CurrentDomain.FriendlyName;
                Console.WriteLine(
    "AppDomain: {0}", currentAppDomainName);
                Console.WriteLine(
    "Echo Input: {0}", input);
                Console.WriteLine();
                Console.WriteLine(
    "Non-GAC assemblies loaded in {0} appDomain:", AppDomain.CurrentDomain.FriendlyName);
                
    foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    
    if (!assembly.GlobalAssemblyCache)
                    {
                        Console.WriteLine(
    "\t" + assembly.GetName().Name);
                    }
                }


                Console.BackgroundColor = originalColor;

                
                
    return input + " from AppDomain: " + currentAppDomainName;

            }

            
    #endregion

        }
    }
    程序的输出结果是:
    AppDomain: NewAppDomain
    Echo Input: Hello

    Non-GAC assemblies loaded in NewAppDomain appDomain:
     ImplementationLibrary
     SharedInterface

    Return:
     Hello from AppDomain: NewAppDomain

    Non-GAC assemblies loaded in Application.vshost.exe appDomain:
     vshost
     Application
     SharedInterface

    ImplementationLibrary should not be loaded.

    另外解释一下Unwarp()函数的作用,例如ObjectHandleAssembly有一个MyType的类:
    // Creates an instance of MyType defined in the assembly called ObjectHandleAssembly.
    ObjectHandle obj = domain.CreateInstance("ObjectHandleAssembly", "MyType");

    // Unwrapps the proxy to the MyType object created in the other AppDomain.
    MyType testObj = (MyType)obj.Unwrap();

  • 相关阅读:
    给JFinal添加 Sqlite 数据库支持
    超强、超详细Redis数据库入门教程
    HTML5实现多文件的上传示例代码
    JAVA 使用Dom4j 解析XML
    【VBA研究】Excel VBA利用ADODB访问数据库使用小结
    JFinal 部署在 Tomcat 下推荐方法
    Java WebService 简单实例
    关于XML的验证(DTD与XSD)一点实践
    基于机器学习的web异常检测
    30万奖金!还带你奔赴加拿大相约KDD!?阿里聚安全算法挑战赛带你飞起!
  • 原文地址:https://www.cnblogs.com/bear831204/p/1315102.html
Copyright © 2011-2022 走看看