• .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();

  • 相关阅读:
    一周的前端面试
    PHP导出超大的CSV格式的Excel表方案
    Java HashMap Demo
    Vmware 设置桥接模式
    Vue 模板
    SpringMVC 拦截器
    IntelliJ IDEA 修改缓存文件设置
    Maven 命令操作项目
    Maven 介绍
    Spring Boot 5 SpringSecurity身份验证
  • 原文地址:https://www.cnblogs.com/bear831204/p/1315102.html
Copyright © 2020-2023  润新知