コード例 #1
0
        public void CreateShuttleDomain()
        {
            // 创建一个 ShuttleDomain
            var key = this.GetType().Name;

            _shuttleDomain = ShuttleDomainHelper.Create(key, key);
        }
コード例 #2
0
        public static ShuttleDomain Create(string assemblySymbol, string assemblyName, ServiceContainer svContainer)
        {
            var dynAsmOptions = new DynamicAssemblyOptions
            {
                AccessMode   = AssemblyBuilderAccess.Run,
                AssemblyName = new AssemblyName(assemblyName)
            };

            var options = new ShuttleDomainOptions
            {
                DynamicAssemblySymbol  = assemblySymbol,
                DynamicAssemblyOptions = dynAsmOptions,
                DefaultLeaseTime       = 10.Seconds(),
                PollingInterval        = 5.Seconds()
            };

            try
            {
                return(ShuttleDomain.Create(ref options, svContainer));
            }
            catch (Exception e)
            {
                if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
                else
                {
                    Console.WriteLine(e.Message);
                }
                return(null);
            }
        }
コード例 #3
0
        static bool CallServicesDefinedInAnotherAssembly(ShuttleDomain shuttleDomain)
        {
            Console.WriteLine();
            Console.WriteLine("=====================================");

            var remoteDomain = AppDomain.CreateDomain(Guid.NewGuid().ToString(), null, null);

            var currentDir           = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            var simpleServiceEndPath = Path.Combine(currentDir, SimpleServiceEndDll);
            var asmName = AssemblyName.GetAssemblyName(simpleServiceEndPath);
            var simpleRemoteServiceEnd = (SimpleRemoteServiceEnd)remoteDomain.CreateInstanceAndUnwrap
                                             (asmName.FullName, SimpleRemoteServiceEndType);

            simpleRemoteServiceEnd.CreateShuttleDomain();
            simpleRemoteServiceEnd.RegisterServices();

            ISimpleService2 service2;

            if (shuttleDomain.TryGetService(out service2))
            {
                try
                {
                    Console.WriteLine("Trying to call a remote serivce that defined in another library from AppDomain {0}...",
                                      AppDomain.CurrentDomain.FriendlyName);

                    Console.WriteLine();
                    // 调用子 AppDomain 中注册的 ISimpleService2 服务实例的服务方法。
                    // Call the service method of ISimpleService2 service.
                    var output = service2.GetOutput("Duang");
                    Console.WriteLine(output);

                    Console.WriteLine();
                }
                catch
                {
                    Console.WriteLine();
                    Console.WriteLine("Failed to invoke the remote service method...");
                    return(false);
                }
            }
            else
            {
                Console.WriteLine();
                Console.WriteLine("Failed to create remote service instance...");
                return(false);
            }

            simpleRemoteServiceEnd.DisposeShuttleDomain();
            return(true);
        }
コード例 #4
0
        public override bool Setup()
        {
            _remoteDomain = AppDomain.CreateDomain(Guid.NewGuid().ToString(), null, null);

            _remoteStarter1 = (RemoteStarter1)_remoteDomain.CreateInstanceAndUnwrap
                                  (typeof(RemoteStarter1).Assembly.FullName, typeof(RemoteStarter1).FullName);
            _remoteStarter1.CreateShuttleDomain();
            _remoteStarter1.RegisterServices();

            var key = Guid.NewGuid().ToString();

            _shuttleDomain = ShuttleDomainHelper.Create(key, key);

            return(true);
        }
コード例 #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Tests begin...");

            // 要使用 JointCode.Shuttle 进行跨 AppDomain 通信,首先必须初始化 ShuttleDomain。
            // 这个初始化操作一般在默认 AppDomain 执行,但也可以在其他 AppDomain 中执行,都是一样的。
            // To make cross-AppDomain communication with JointCode.Shuttle, initialize the ShuttleDomain at first.
            // It doesn't matter whether the initialization is done in default AppDomain or any other AppDomains,
            // but it must be done before any ShuttleDomain instance is created.
            ShuttleDomain.Initialize();

            // 在默认 AppDomain 中,创建一个 ShuttleDomain。
            // 事实上,在需要与其他 AppDomain 进行通信的每个 AppDomain 中,都要有一个且只能有一个 ShuttleDomain 对象。
            // 尝试在一个 AppDomain 中创建多个 ShuttleDomain 对象时将会抛出异常。
            // 该对象用于与其他 AppDomain 中的 ShuttleDomain 对象通信。
            // Creating a ShuttleDomain instance in default AppDomain.
            // Actually, we needs one and only one ShuttleDomain instance in every AppDomain that needs to communicate
            // with others. Trying to create another ShuttleDomain in the same AppDomain causes exceptions.
            // The ShuttleDomain instances communicates with each other across AppDomains.
            var str           = Guid.NewGuid().ToString();
            var shuttleDomain = ShuttleDomainHelper.Create(str, str);

            if (CallServicesDefinedInThisAssembly(shuttleDomain) &&
                CallServicesDefinedInAnotherAssembly(shuttleDomain))
            {
                Console.WriteLine("Tests completed...");
            }
            else
            {
                Console.WriteLine("Tests failed...");
            }

            shuttleDomain.Dispose();

            Console.Read();
        }
コード例 #6
0
        static bool CallServicesDefinedInThisAssembly(ShuttleDomain shuttleDomain)
        {
            Console.WriteLine();
            Console.WriteLine("=====================================");

            // 在默认 AppDomain 中创建一个子 AppDomain。
            // Creating a child AppDomain in default AppDomain.
            var serviceEnd1Domain = AppDomain.CreateDomain("ServiceEndDomain1", null, null);

            // 创建一个 ServiceEnd 对象以用于操作该子 AppDomain。
            // Creating a ServiceEnd instance for operating that child AppDomain.
            var serviceEnd = (ServiceEnd)serviceEnd1Domain.CreateInstanceAndUnwrap
                                 (typeof(Program).Assembly.FullName, "JoitCode.Shuttle.SimpleSample.ServiceEnd");

            // 在子 AppDomain 中,创建一个 ShuttleDomain 实例。
            // Creating a ShuttleDomain instance in the child AppDomain.
            serviceEnd.CreateShuttleDomain();

            // 在子 AppDomain 中,注册 ISimpleService 服务。
            // Registering ISimpleService service in the child AppDomain.
            serviceEnd.RegisterServices();


            // 在默认 AppDomain 中,获取子 AppDomain 中注册的 ISimpleService 服务实例。
            // 目前服务实例的默认生存期为 1 分钟。每次调用服务方法时,服务实例的生存期延长 30 秒。
            // Get the ISimpleService service in default AppDomain, which is registered by the child AppDomain.
            // The lifetime of service is default to 1 minute, every call to the service method extends that time for 30 seconds.
            ISimpleService service;

            if (shuttleDomain.TryGetService(out service))
            {
                try
                {
                    Console.WriteLine("Currently, we are running in AppDomain {0}, " +
                                      "and we are trying to call a remote serivce that defined in the same library...",
                                      AppDomain.CurrentDomain.FriendlyName);

                    Console.WriteLine();
                    // 调用子 AppDomain 中注册的 ISimpleService 服务实例的服务方法。
                    // Call the service method of ISimpleService service.
                    var output = service.GetOutput("Bingo");
                    Console.WriteLine(output);

                    Console.WriteLine();
                }
                catch
                {
                    Console.WriteLine();
                    Console.WriteLine("Failed to invoke the remote service method...");
                    return(false);
                }
            }
            else
            {
                Console.WriteLine();
                Console.WriteLine("Failed to create remote service instance...");
                return(false);
            }

            // 通知子 AppDomain 立即释放 ISimpleService 服务实例,而不用等待其生存期结束。
            // 此为可选操作,因为即使不手动释放 ISimpleService 服务实例,在其生命期结束之时系统也会自动释放该实例
            //(如果 ISimpleService 实现了 IDisposable,还会调用其 Dispose 方法)
            // Indicating the child AppDomain to release the ISimpleService service immediately, instead of waiting for its lifetime to end.
            // This is optional, because even if we don't do this explicitly, the ISimpleService service will still get released in the
            // child AppDomain automatically when its lifetime ends.
            // And, if the ISimpleService derives from IDisposable, the Dispose method will also get called at that time.
            shuttleDomain.ReleaseService(service);

            // 在子 AppDomain 中,释放缓存的 ShuttleDomain 实例。这将会注销通过该实例注册的所有服务(在本示例中,即 ISimpleService 服务),
            // 并切断该 AppDomain 与所有 AppDomain 的通信。
            // Releasing the ShuttleDomain instance in the child AppDomain, this will unregister all services registered by that
            // instance, and shut down all communications between that child AppDomain and all other AppDomains.
            serviceEnd.DisposeShuttleDomain();

            return(true);
        }
コード例 #7
0
 public void CreateShuttleDomain()
 {
     // 创建一个 ShuttleDomain
     // Create a ShuttleDomain object
     _shuttleDomain = ShuttleDomainHelper.Create("domain1", "domain1");
 }
コード例 #8
0
ファイル: Program.cs プロジェクト: vebin/JointCode.Shuttle
        static void Main(string[] args)
        {
            _tests.AddRange(new Test[]
            {
                new ShuttleDomainFunctionalTest(),
                new ShuttleDomainPerformanceTest(),
                new MarshalByObjectPerformanceTest(),
                new ShuttleDomainLifeTimeManagementTest(),
                new ShuttleDomainAnyAppDomainAccessTest(),
                new MarshalByRefCrossAccessTest(),
                new ShuttleDomainServiceUpdateTest(),
            });

            // 显示测试说明
            PrintNotification();

            // 初始化 ShuttleDomain
            ShuttleDomain.Initialize();

            // 初始化性能测量组件
            CodeTimer.Initialize();

            Console.WriteLine();
            Console.WriteLine("Beginning Tests...");
            Console.WriteLine("=========================================================================");

            do
            {
                if (_reEnter)
                {
                    var input = Console.ReadLine();
                    if (String.Equals("x", input, StringComparison.InvariantCultureIgnoreCase))
                    {
                        break;
                    }
                }

                Console.WriteLine("Test List (SD stands for ShuttleDomain, MBO stands for MarshalByRefObject)");
                Console.WriteLine("=========================================================================");

                var testInfo = BuildTestInfo();
                Console.Write(testInfo);

                Console.WriteLine
                    ("Input the index of test to run:");
                int index;
                var strIndex = Console.ReadLine();
                if (!int.TryParse(strIndex, out index))
                {
                    throw new InvalidOperationException(string.Format("The [{0}] is not a number!", strIndex));
                }
                if (index > _tests.Count - 1)
                {
                    throw new InvalidOperationException(string.Format("The specified index [{0}] is out of range!", strIndex));
                }

                var test = _tests[index];
                test.Run();

                Console.WriteLine();
                var oldForeColor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine
                    ("press [x] to exit the tests,or [c] to run other tests.");
                Console.ForegroundColor = oldForeColor;
                Console.WriteLine();

                _reEnter = true;
            } while (true);

            Console.WriteLine();
            Console.WriteLine("Tests completed!");
            Console.Read();
        }