// acquires the main domain private bool Acquire() { // if signaled, too late to acquire, give up // the handler is not installed so that would be the hosting environment if (_signaled) { _logger.Info <MainDom>("Cannot acquire (signaled)."); return(false); } _logger.Info <MainDom>("Acquiring."); // Get the lock var acquired = false; try { acquired = _mainDomLock.AcquireLockAsync(LockTimeoutMilliseconds).GetAwaiter().GetResult(); } catch (Exception ex) { _logger.Error <MainDom>(ex, "Error while acquiring"); } if (!acquired) { _logger.Info <MainDom>("Cannot acquire (timeout)."); // In previous versions we'd let a TimeoutException be thrown // and the appdomain would not start. We have the opportunity to allow it to // start without having MainDom? This would mean that it couldn't write // to nucache/examine and would only be ok if this was a super short lived appdomain. // maybe safer to just keep throwing in this case. throw new TimeoutException("Cannot acquire MainDom"); // return false; } try { // Listen for the signal from another AppDomain coming online to release the lock _listenTask = _mainDomLock.ListenAsync(); _listenCompleteTask = _listenTask.ContinueWith(t => { _logger.Debug <MainDom>("Listening task completed with {TaskStatus}", _listenTask.Status); OnSignal("signal"); }, TaskScheduler.Default); // Must explicitly specify this, see https://blog.stephencleary.com/2013/10/continuewith-is-dangerous-too.html } catch (OperationCanceledException ex) { // the waiting task could be canceled if this appdomain is naturally shutting down, we'll just swallow this exception _logger.Warn <MainDom>(ex, ex.Message); } _logger.Info <MainDom>("Acquired."); return(true); }
// acquires the main domain private bool Acquire() { // if signaled, too late to acquire, give up // the handler is not installed so that would be the hosting environment if (_signaled) { _logger.Info <MainDom>("Cannot acquire (signaled)."); return(false); } _logger.Info <MainDom>("Acquiring."); // Get the lock var acquired = _mainDomLock.AcquireLockAsync(LockTimeoutMilliseconds).GetAwaiter().GetResult(); if (!acquired) { _logger.Info <MainDom>("Cannot acquire (timeout)."); // In previous versions we'd let a TimeoutException be thrown // and the appdomain would not start. We have the opportunity to allow it to // start without having MainDom? This would mean that it couldn't write // to nucache/examine and would only be ok if this was a super short lived appdomain. // maybe safer to just keep throwing in this case. throw new TimeoutException("Cannot acquire MainDom"); // return false; } try { // Listen for the signal from another AppDomain coming online to release the lock _mainDomLock.ListenAsync().ContinueWith(_ => OnSignal("signal")); } catch (OperationCanceledException ex) { // the waiting task could be canceled if this appdomain is naturally shutting down, we'll just swallow this exception _logger.Warn <MainDom>(ex, ex.Message); } _logger.Info <MainDom>("Acquired."); return(true); }