Inheritance: System.MarshalByRefObject, ISponsor
 protected virtual void Dispose(bool isDisposing)
 {
     if (isDisposing)
     {
         _clientSponsor?.Close();
         _clientSponsor = null;
     }
 }
        private void SetLeases()
        {
            _clientSponsor?.Close();
            _clientSponsor = new ClientSponsor(TimeSpan.MaxValue);
            _clientSponsor.Register((MarshalByRefObject) _missionPlannerInterfaces.CurrentState);
            //_clientSponsor.Register((MarshalByRefObject) _missionPlannerInterfaces.FlightComms);
            _clientSponsor.Register((MarshalByRefObject) _missionPlannerInterfaces.MissionPlanner);

            var lease = (ILease)_clientSponsor.InitializeLifetimeService();
            _clientSponsor.Renewal(lease);
        }
		/// <summary>
		/// Creates the component instance by calling the <see cref="RemoteLoader.CreateRemoteInstance"/>
		/// method. The component is then registered with the <see cref="ClientSponsor"/>
		/// with a renewal time of 2 minutes, in order to stay alive forever.
		/// </summary>
		protected override object CreateInstance(CreationContext context, object[] arguments, Type[] signature)
		{
			object instance = loader.CreateRemoteInstance(Model, context, arguments, signature);

			if (keepAliveSponsor == null)
				keepAliveSponsor = new ClientSponsor(sponsorTimeout);

			MarshalByRefObject mbro = (MarshalByRefObject) instance;
			ILease lease = (ILease) RemotingServices.GetLifetimeService(mbro);
			if (lease != null)
			{
				lease.Register(keepAliveSponsor);
				lease.Renew(sponsorTimeout);
			}

			return instance;
		}
Beispiel #4
0
        public void Start(ZetboxConfig config)
        {
            if (config == null) { throw new ArgumentNullException("config"); }

            using (Logging.Log.DebugTraceMethodCall("Start", "Starting AppDomain for Server"))
            {
                var serverConfig = new ZetboxConfig()
                {
                    ConfigFilePath = config.ConfigFilePath,
                    ConfigName = config.ConfigName,
                    EnableShadowCopy = config.EnableShadowCopy,
                    HostType = HostType.Server,
                    IsFallback = false,
                    Server = config.Server,
                };

                serverDomain = AppDomain.CreateDomain("ServerAppDomain",
                    AppDomain.CurrentDomain.Evidence,
                    AppDomain.CurrentDomain.SetupInformation);

                AssemblyLoader.Bootstrap(serverDomain, serverConfig);

                serverManager = (IZetboxAppDomain)serverDomain.CreateInstanceAndUnwrap(
                    "Zetbox.Server.Service",
                    "Zetbox.Server.Service.ServerManager");
                serverManager.Start(serverConfig);

                if (clientSponsor == null)
                {
                    clientSponsor = new ClientSponsor();
                    clientSponsor.RenewalTime = TimeSpan.FromMinutes(2);
                }

                clientSponsor.Register(serverManager as MarshalByRefObject);
            }
        }
        public void Start(ZetboxConfig config)
        {
            using (Logging.Log.DebugTraceMethodCall("Start", "Starting AppDomain for Server"))
            {
                serverDomain = AppDomain.CreateDomain("ServerAppDomain",
                    AppDomain.CurrentDomain.Evidence,
                    AppDomain.CurrentDomain.SetupInformation);

                AssemblyLoader.Bootstrap(serverDomain, config);

                serverManager = (IZetboxAppDomain)serverDomain.CreateInstanceAndUnwrap(
                    "Zetbox.Server.Service",
                    "Zetbox.Server.Service.ServerManager");
                serverManager.Start(config);

                if (clientSponsor == null)
                {
                    clientSponsor = new ClientSponsor();
                    clientSponsor.RenewalTime = TimeSpan.FromMinutes(2);
                }

                clientSponsor.Register(serverManager as MarshalByRefObject);
            }
        }
        /// <summary>
        /// Indicates to the TaskHost that it is no longer needed.
        /// Called by TaskBuilder when the task using the EngineProxy is done.
        /// </summary>
        internal void MarkAsInactive()
        {
            VerifyActiveProxy();
            _activeProxy = false;

            _loggingContext = null;
            _elementLocation = null;

            // Clear out the sponsor (who is responsible for keeping the EngineProxy remoting lease alive until the task is done)
            // this will be null if the engineproxy was never sent across an appdomain boundry.
            if (_sponsor != null)
            {
                ILease lease = (ILease)RemotingServices.GetLifetimeService(this);

                if (lease != null)
                {
                    lease.Unregister(_sponsor);
                }

                _sponsor.Close();
                _sponsor = null;
            }
        }
        /// <summary>
        /// InitializeLifetimeService is called when the remote object is activated. 
        /// This method will determine how long the lifetime for the object will be.
        /// </summary>
        /// <returns>The lease object to control this object's lifetime.</returns>
        public override object InitializeLifetimeService()
        {
            VerifyActiveProxy();

            // Each MarshalByRef object has a reference to the service which
            // controls how long the remote object will stay around
            ILease lease = (ILease)base.InitializeLifetimeService();

            // Set how long a lease should be initially. Once a lease expires
            // the remote object will be disconnected and it will be marked as being availiable 
            // for garbage collection
            int initialLeaseTime = 1;

            string initialLeaseTimeFromEnvironment = Environment.GetEnvironmentVariable("MSBUILDENGINEPROXYINITIALLEASETIME");

            if (!String.IsNullOrEmpty(initialLeaseTimeFromEnvironment))
            {
                int leaseTimeFromEnvironment;
                if (int.TryParse(initialLeaseTimeFromEnvironment, out leaseTimeFromEnvironment) && leaseTimeFromEnvironment > 0)
                {
                    initialLeaseTime = leaseTimeFromEnvironment;
                }
            }

            lease.InitialLeaseTime = TimeSpan.FromMinutes(initialLeaseTime);

            // Make a new client sponsor. A client sponsor is a class
            // which will respond to a lease renewal request and will
            // increase the lease time allowing the object to stay in memory
            _sponsor = new ClientSponsor();

            // When a new lease is requested lets make it last 1 minutes longer. 
            int leaseExtensionTime = 1;

            string leaseExtensionTimeFromEnvironment = Environment.GetEnvironmentVariable("MSBUILDENGINEPROXYLEASEEXTENSIONTIME");
            if (!String.IsNullOrEmpty(leaseExtensionTimeFromEnvironment))
            {
                int leaseExtensionFromEnvironment;
                if (int.TryParse(leaseExtensionTimeFromEnvironment, out leaseExtensionFromEnvironment) && leaseExtensionFromEnvironment > 0)
                {
                    leaseExtensionTime = leaseExtensionFromEnvironment;
                }
            }

            _sponsor.RenewalTime = TimeSpan.FromMinutes(leaseExtensionTime);

            // Register the sponsor which will increase lease timeouts when the lease expires
            lease.Register(_sponsor);

            return lease;
        }
Beispiel #8
0
        public void Stop()
        {
            lock (_lock)
            {
                if (serverManager != null)
                {
                    try
                    {
                        clientSponsor.Unregister(serverManager as MarshalByRefObject);
                        Logging.Log.Info("Closing client sponsor");
                        clientSponsor.Close();
                        Logging.Log.Info("Stopping server manager");
                        serverManager.Stop();
                        Logging.Log.Info("Unloading AssemblyLoader from server app domain");
                        AssemblyLoader.Unload(serverDomain);
                    }
                    catch (Exception ex)
                    {
                        Logging.Log.Warn("Error during shutdown", ex);
                        throw;
                    }
                }
                else
                {
                    Logging.Log.Error("Tried unloading an already unloaded server manager.");
                }

                clientSponsor = null;
                serverManager = null;

                if (serverDomain != null)
                {
                    Logging.Log.Info("Unloading server app domain");
                    AppDomain.Unload(serverDomain);
                }
                else
                {
                    Logging.Log.Warn("Server app domain already vanished.");
                }

                serverDomain = null;
            }
        }
            public void KeepRemoteObjectAlive()
            {
                sum = CSScript.Evaluator
                              .CreateDelegateRemotely(@"int Sum(int a, int b)
                                                        {
                                                            return a+b;
                                                        }");

                //Normally remote objects are disposed if they are not accessed withing a default timeout period.
                //It is not even enough to keep transparent proxies or their wrappers (e.g. 'sum') referenced. 
                //To prevent GC collection in the remote domain use .NET ClientSponsor mechanism as below.
                sumSponsor = sum.ExtendLifeFromMinutes(30);
            }
 protected void UnregisterRemoteObject()
 {
     try
     {
         if(m_sponsor != null)
         {
             m_sponsor.Unregister( m_mbr );            
         }                
     }
     catch
     {
     }
     
     m_sponsor = null;
     m_mbr     = null;
 }
        //--//

        protected void RegisterRemoteObject(ServerRemoteObject mbr)
        {
            UnregisterRemoteObject();

            m_mbr     = mbr;
            m_sponsor = new ClientSponsor();
            m_sponsor.Register( mbr );            
        }
Beispiel #12
0
 public void BuildDocument()
 {
     var sponsor = new ClientSponsor();
     if (_listener != null)
     {
         Logger.RegisterListener(_listener);
         sponsor.Register(_listener);
     }
     try
     {
         BuildDocument(_config, _baseDirectory, _outputDirectory, _pluginDirectory);
     }
     finally
     {
         sponsor.Close();
     }
 }
Beispiel #13
0
        /// <summary>
        /// Indicates to the TaskHost that it is no longer needed.
        /// Called by TaskBuilder when the task using the EngineProxy is done.
        /// </summary>
        internal void MarkAsInactive()
        {
            lock (_callbackMonitor)
            {
                VerifyActiveProxy();
                _activeProxy = false;

                // Since the task has a pointer to this class it may store it in a static field. Null out
                // internal data so the leak of this object doesn't lead to a major memory leak.            
                _host = null;
                _requestEntry = null;

                // Don't bother clearing the tiny task location
                _taskLoggingContext = null;
                _targetBuilderCallback = null;

                // Clear out the sponsor (who is responsible for keeping the EngineProxy remoting lease alive until the task is done)
                // this will be null if the engineproxy was never sent across an appdomain boundry.
                if (_sponsor != null)
                {
                    ILease lease = (ILease)RemotingServices.GetLifetimeService(this);

                    if (lease != null)
                    {
                        lease.Unregister(_sponsor);
                    }

                    _sponsor.Close();
                    _sponsor = null;
                }
            }
        }
Beispiel #14
0
        /// <summary>
        /// Notifies this object that its work is done.
        /// Thread safe. However, InitializeLifetimeService and MarkAsInactive should
        /// only be called in that order, together or not at all, and no more than once.
        /// </summary>
        /// <remarks>
        /// Indicates to the TaskLoggingHelper that it is no longer needed.
        /// </remarks>
        public void MarkAsInactive()
        {
            lock (_locker)
            {
                // Clear out the sponsor (who is responsible for keeping the TaskLoggingHelper remoting lease alive until the task is done)
                // this will be null if the engineproxy was never sent across an appdomain boundry.
                if (_sponsor != null)
                {
                    ILease lease = (ILease)RemotingServices.GetLifetimeService(this);

                    if (lease != null)
                    {
                        lease.Unregister(_sponsor);
                    }

                    _sponsor.Close();
                    _sponsor = null;
                }
            }
        }
Beispiel #15
0
        /// <summary>
        /// Indicates to the EngineProxy that it is no longer needed.
        /// Called by TaskEngine when the task using the EngineProxy is done.
        /// </summary>
        internal void MarkAsInActive()
        {
            activeProxy = false;

            // Since the task has a pointer to this class it may store it in a static field. Null out
            // internal data so the leak of this object doesn't lead to a major memory leak.
            loggingServices = null;
            parentModule = null;
            buildEventContext = null;
            
            // Clear out the sponsor (who is responsible for keeping the EngineProxy remoting lease alive until the task is done)
            // this will be null if the engineproxy was never sent accross an appdomain boundry.
            if (sponsor != null)
            {
                ILease lease = (ILease)RemotingServices.GetLifetimeService(this);
             
                if (lease != null)
                {
                    lease.Unregister(sponsor);
                }
                
                sponsor.Close();
                sponsor = null;
            }
        }
Beispiel #16
0
        private void StartInDomain()
        {
            GallatinEventLog.WriteEntry( "Starting Gallatin Proxy", EventLogEntryType.Information );

            AppDomainSetup appDomainSetup = new AppDomainSetup();
            appDomainSetup.ApplicationName = "GallatinProxyAppDomain";
            appDomainSetup.ShadowCopyFiles = "true";

            _domain = AppDomain.CreateDomain( "GallatinDomain", AppDomain.CurrentDomain.Evidence, appDomainSetup );
            _domain.UnhandledException += HandleDomainUnhandledException;
            _domain.InitializeLifetimeService();
            _service = (IProxyService) _domain.CreateInstanceAndUnwrap( "Gallatin.Core", "Gallatin.Core.Service.CrossDomainProxyService" );
            _service.Start();

            MarshalByRefObject marshalByRefObject = _service as MarshalByRefObject;
            if ( marshalByRefObject == null )
            {
                throw new InvalidCastException( "Unable to cast service as a MarshalByRefObject" );
            }

            _sponsor = new ClientSponsor();
            _sponsor.Register( marshalByRefObject );
        }