Caliburn-Micro / Caliburn.Micro

A small, yet powerful framework, designed for building applications across all XAML platforms. Its strong support for MV* patterns will enable you to build your solution quickly, without the need to sacrifice code quality or testability.
http://caliburnmicro.com/
MIT License
2.79k stars 775 forks source link

DisplayRootViewForAsync throws System.InvalidOperationException #894

Closed vugarli closed 8 months ago

vugarli commented 8 months ago

I setted up IoC container as instructed here.

Here is my CustomBootstrapper.cs:

public record Test(string name);

public class CustomBootstrapper : BootstrapperBase
{
    private SimpleContainer _container = new SimpleContainer();

    protected override object GetInstance(Type serviceType, string key)
    {
        return _container.GetInstance(serviceType, key);
    }

    protected override IEnumerable<object> GetAllInstances(Type serviceType)
    {
        return _container.GetAllInstances(serviceType);
    }

    protected override void BuildUp(object instance)
    {
        _container.BuildUp(instance);
    }

    protected override void Configure()
    {
        _container.Singleton<IWindowManager, WindowManager>();
        _container.Singleton<Test>();
        _container.Handler<Test>( (c) => new Test("aaa"));
        _container.RegisterPerRequest(typeof(ShellViewModel), null, typeof(ShellViewModel));
    }

    public CustomBootstrapper()
    {
        Initialize();
    }

    protected async override void OnStartup(object sender, StartupEventArgs e)
    {
        base.OnStartup(sender, e);
        await DisplayRootViewForAsync<ShellViewModel>();
    }

}

Call to DisplayRootViewForAsync() throws System.InvalidOperationException. Here is StackTrace:

   at System.Linq.ThrowHelper.ThrowMoreThanOneElementException() in /_/src/libraries/System.Linq/src/System/Linq/ThrowHelper.cs:line 18
   at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found) in /_/src/libraries/System.Linq/src/System/Linq/Single.cs:line 104
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source) in /_/src/libraries/System.Linq/src/System/Linq/Single.cs:line 13
   at Caliburn.Micro.SimpleContainer.GetInstance(Type service, String key) in D:\a\1\s\src\Caliburn.Micro.Core\SimpleContainer.cs:line 109
   at Caliburn.Micro.SimpleContainer.<DetermineConstructorArgs>b__29_0(ParameterInfo info) in D:\a\1\s\src\Caliburn.Micro.Core\SimpleContainer.cs:line 290
   at System.Linq.Enumerable.SelectArrayIterator`2.MoveNext() in /_/src/libraries/System.Linq/src/System/Linq/Select.cs:line 191
   at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection) in /_/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs:line 269
   at Caliburn.Micro.SimpleContainer.DetermineConstructorArgs(Type implementation) in D:\a\1\s\src\Caliburn.Micro.Core\SimpleContainer.cs:line 290
   at Caliburn.Micro.SimpleContainer.BuildInstance(Type type) in D:\a\1\s\src\Caliburn.Micro.Core\SimpleContainer.cs:line 261
   at Caliburn.Micro.SimpleContainer.<>c__DisplayClass13_0.<RegisterPerRequest>b__0(SimpleContainer container) in D:\a\1\s\src\Caliburn.Micro.Core\SimpleContainer.cs:line 58
   at Caliburn.Micro.SimpleContainer.GetInstance(Type service, String key) in D:\a\1\s\src\Caliburn.Micro.Core\SimpleContainer.cs:line 109
   at FileExplorer.CustomBootstrapper.GetInstance(Type serviceType, String key) in C:\Users\vuqar\Desktop\Code\WPF\FileExplorer\CustomBootstrapper.cs:line 24
   at Caliburn.Micro.BootstrapperBase.<DisplayRootViewForAsync>d__19.MoveNext() in D:\a\1\s\src\Caliburn.Micro.Platform\Platforms\net46-netcore\Bootstrapper.cs:line 209
   at FileExplorer.CustomBootstrapper.<OnStartup>d__6.MoveNext() in C:\Users\vuqar\Desktop\Code\WPF\FileExplorer\CustomBootstrapper.cs:line 54
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state) in /_/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:line 1914
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/Threading/ExceptionWrapper.cs:line 110
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/Threading/ExceptionWrapper.cs:line 36
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs:line 2867
   at System.Windows.Threading.DispatcherOperation.InvokeImpl() in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/DispatcherOperation.cs:line 540
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/DispatcherOperation.cs:line 487
   at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj) in /_/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/CulturePreservingExecutionContext.cs:line 214
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) in /_/src/libraries/System.Private.CoreLib/src/System/Threading/ExecutionContext.cs:line 179
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) in /_/src/libraries/System.Private.CoreLib/src/System/Threading/ExecutionContext.cs:line 203
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) in /_/src/libraries/System.Private.CoreLib/src/System/Threading/ExecutionContext.cs:line 143
   at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state) in /_/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/CulturePreservingExecutionContext.cs:line 172
   at System.Windows.Threading.DispatcherOperation.Invoke() in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/DispatcherOperation.cs:line 409
   at System.Windows.Threading.Dispatcher.ProcessQueue() in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs:line 2093
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs:line 2304
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) in /_/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndWrapper.cs:line 295
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) in /_/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndSubclass.cs:line 429
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/Threading/ExceptionWrapper.cs:line 103
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/Threading/ExceptionWrapper.cs:line 36
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs:line 2867
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs:line 1339
   at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg) in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs:line 1058
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) in /_/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndSubclass.cs:line 341
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.TranslateAndDispatchMessage(MSG& msg) in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs:line 2281
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs:line 2143
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs:line 336
   at System.Windows.Threading.Dispatcher.Run() in /_/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs:line 304
   at System.Windows.Application.RunDispatcher(Object ignore) in /_/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs:line 2406
   at System.Windows.Application.RunInternal(Window window) in /_/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs:line 1693
   at System.Windows.Application.Run(Window window) in /_/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs:line 220
   at System.Windows.Application.Run() in /_/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs:line 198
   at FileExplorer.App.Main()

It seems like there is more than one instance of ShellViewModel registered to IoC? Any ideas why this is happening or how to fix it?

vugarli commented 8 months ago

I registered same type twice in Configure()