public async Task Test_StartupException()
        {
            bool isDisposed = false;

            AssemblyUtils.SetEntryAssembly(GetType().Assembly);

            var applicationConfig = ApplicationConfigBuilder
                                    .Create()
                                    .WithApplicationName(ApplicationName)
                                    .WithScanDirectories(
                TestAssemblyDirectories
                )
                                    // Add all file starting with Dapplo and ending on .dll
                                    .WithAssemblyPatterns("Dapplo*")
                                    .BuildApplicationConfig();

            using (var bootstrapper = new ApplicationBootstrapper(applicationConfig))
            {
                bootstrapper.Configure();

                // Makes the startup break
                bootstrapper.Builder.Register(context => true);

                bootstrapper.RegisterForDisposal(SimpleDisposable.Create(() => isDisposed = true));

                // Initialize, so we can export
                Assert.True(await bootstrapper.InitializeAsync().ConfigureAwait(false), "Not initialized");

                // Start the composition, and IStartupActions
                await Assert.ThrowsAsync <NotSupportedException>(async() => await bootstrapper.StartupAsync().ConfigureAwait(false));
            }
            // Dispose automatically calls IShutdownActions
            Assert.True(isDisposed);
        }
Example #2
0
    public IDisposable Subscribe(IObserver <object> observer)
    {
        PropertyChangedEventHandler evt = delegate { observer.OnNext(ObjectValue); };

        PropertyChanged += evt;
        var disposer = new SimpleDisposable(() => PropertyChanged -= evt);

        if (Owner != null)
        {
            Owner.AddBinding(disposer);
        }
        return(disposer);
    }
Example #3
0
        public void All()
        {
            // Dispose many times
            int count = 0;
            var obj   = new SimpleDisposable(() => count += 1);

            Assert.Equals(count, 0);
            obj.Dispose();
            Assert.Equals(count, 1);
            obj.Dispose();
            Assert.Equals(count, 1);
            // With using directive
            count = 0;
            using (new SimpleDisposable(() => count += 1)) {
            }
            Assert.Equals(count, 1);
        }
        public void All()
        {
            // Dispose many times
            int count = 0;
            var obj   = new SimpleDisposable(() => count += 1);

            Assert.Equals(count, 0);
            obj.Dispose();
            Assert.Equals(count, 1);
#pragma warning disable S3966 // Objects should not be disposed more than once
            obj.Dispose();
#pragma warning restore S3966 // Objects should not be disposed more than once
            Assert.Equals(count, 1);
            // With using directive
            count = 0;
            using (new SimpleDisposable(() => count += 1)) {
                // Do nothing
            }
            Assert.Equals(count, 1);
        }
Example #5
0
        /// <summary>
        /// Create an observable for the INotifyPropertyChanged
        /// </summary>
        /// <param name="source">INotifyPropertyChanged</param>
        /// <param name="propertyNamePattern">Optional property name / pattern</param>
        /// <typeparam name="T">INotifyPropertyChanged</typeparam>
        /// <returns>IObservable with PropertyChangedEventArgs</returns>
        public static IObservable <PropertyChangedEventArgs> OnPropertyChanged <T>(this T source, string propertyNamePattern = null)
            where T : INotifyPropertyChanged
        {
            var observable = Observable.Create <PropertyChangedEventArgs>(observer =>
            {
                PropertyChangedEventHandler handler = (s, e) => observer.OnNext(e);
                source.PropertyChanged += handler;
                return(SimpleDisposable.Create(() => source.PropertyChanged -= handler));
            });

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            // Create predicate
            Func <PropertyChangedEventArgs, bool> predicate;

            if (!string.IsNullOrEmpty(propertyNamePattern) && propertyNamePattern != "*")
            {
                predicate = propertyChangedEventArgs =>
                {
                    try
                    {
                        var propertyName = propertyChangedEventArgs.PropertyName;
                        return(string.IsNullOrEmpty(propertyName) || propertyName == "*" || propertyNamePattern == propertyName || Regex.IsMatch(propertyName, propertyNamePattern));
                    }
                    catch (Exception ex)
                    {
                        Log.Error().WriteLine(ex, "Error in predicate for OnPropertyChangedPattern");
                    }
                    return(false);
                };
            }
            else
            {
                predicate = args => true;
            }

            return(observable.Where(predicate));
        }