예제 #1
0
 public override void RunTest()
 {
     _shuttleDomain.TryGetService(out _lifetimeService);
     _shuttleDomain.TryGetService(out _singletonService);
     _lifetimeService.Execute();
     _singletonService.Execute();
 }
예제 #2
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);
        }
예제 #3
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);
        }