title: The same transient instance it's get from container when instance it's created the second time
body: <p>I'm trying to developing a WPF application by follow the MVVM pattern and by using Inversion of Control.
  I'm using Simple Injector as my IoC container.</p>
  <p>I have to implement something similar to Ambient Scope for <code>DbContext</code>.... basically every time I need to call a service method that interact with data a new UoW instance it's released and that instance will be used through all the Dataservices that implement database operation.</p>
  <p>Something like this like this:</p>
  <pre class="lang-cs prettyprint-override"><code>// Example of Service method 
  
  [STAThread]
  static void Main()
  {
      _container = Bootstrap();
      Debug.WriteLine(CultureInfo.CurrentCulture);
  
      Thread.CurrentThread.CurrentCulture = new CultureInfo(&quot;en-Us&quot;);
      Thread.CurrentThread.CurrentUICulture = new CultureInfo(&quot;en-US&quot;);
  
     
      _app = new App();
      _app.InitializeComponent();
  
      var mainWindow = _container.GetInstance&lt;MainWindow&gt;();
      mainWindow.Closed += MainWindow_Closed;
      _app.Run(mainWindow);
  }
  
  public partial class MainWindow : Window
  {
      public MainWindow(MainWindowViewModel viewModel)
      {
          InitializeComponent();
  
          DataContext = viewModel;
      }
  }
  </code></pre>
  <pre class="lang-xml prettyprint-override"><code>&lt;Window x:Class=&quot;WpfRadDispenser.MainWindow&quot;
          xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
          xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
          xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
          xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
          xmlns:userControls=&quot;clr-namespace:WpfRadDispenser.UserControls&quot;
          xmlns:vm=&quot;clr-namespace:WpfRadDispenser.ViewModel&quot;
          mc:Ignorable=&quot;d&quot;
          Title=&quot;WpfRadDispenser&quot; Height=&quot;768&quot; Width=&quot;1024&quot; 
          d:DataContext=&quot;{d:DesignInstance vm:MainWindowViewModel}&quot;&gt;
  
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
              &lt;RowDefinition Height=&quot;*&quot;/&gt;
              &lt;RowDefinition Height=&quot;*&quot;/&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;userControls:NavigationBar Grid.Row=&quot;0&quot; DataContext=&quot;{Binding Navigator}&quot;/&gt;
          &lt;ContentControl Grid.Row=&quot;1&quot; Content=&quot;{Binding Navigator.CurrentViewModel}&quot;/&gt;
          &lt;ContentControl Grid.Row=&quot;2&quot; Content=&quot;{Binding Navigator.HeaderViewModel}&quot;/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  
  </code></pre>
  <pre class="lang-cs prettyprint-override"><code>
   public class Navigator : ObservableObject, INavigator 
  {
      private readonly IViewModelAbstractFactory _abstractFactory;
      private ViewModelBase _currentViewModel;
      private ViewModelBase _headerViewModel;
  
      public Navigator(IViewModelAbstractFactory abstractFactory)
      {
          _abstractFactory = abstractFactory;
      }
  
      public ViewModelBase CurrentViewModel
      {
          get
          {
              return _currentViewModel;
          }
          set
          {
              _currentViewModel = value;
              OnPropertyChanged();
          }
      }
  
      public ViewModelBase HeaderViewModel
      {
          get
          {
              return _headerViewModel;
          }
  
          set
          {
              _headerViewModel = value;
              OnPropertyChanged();
          }
      }
  
      public ICommand UpdateCurrentViewModelCommand =&gt; new UpdateCurrentViewModelCommand(this, _abstractFactory);
      public ICommand UpdateHeaderViewModelCommand =&gt; new UpdateHeaderViewModelCommand(this, _abstractFactory);
  }
  
  public class UpdateHeaderViewModelCommand : ICommand
  {
      private INavigator _navigator;
      private readonly IViewModelAbstractFactory _viewModelAbstractFactory;
  
      public UpdateHeaderViewModelCommand(INavigator navigator, IViewModelAbstractFactory viewModelAbstractFactory)
      {
          _navigator = navigator;
          _viewModelAbstractFactory = viewModelAbstractFactory;
      }
  
      public bool CanExecute(object parameter)
      {
          return true;
      }
  
      public void Execute(object parameter)
      {
          if (parameter is MenuViewType viewType)
          {
              _navigator.HeaderViewModel = _viewModelAbstractFactory.CreateViewModel(viewType);
          }
      }
  
      public event EventHandler CanExecuteChanged;
  }
  
  public class HeaderViewModelFactory : IWpfRadDispenserViewModelFactory&lt;HeaderviewModel&gt;
  {
      private readonly ProductionService _service;
  
      public HeaderViewModelFactory(ProductionService service)
      {
          _service = service;
      }
  
      public HeaderviewModel CreateViewModel()
      {
          return new HeaderviewModel(_service);
      }
  }
  
  public class HeaderviewModel : ViewModelBase
  {
      private readonly ProductionService _service;
      
      public HeaderviewModel(ProductionService service)
      {
          _service = service;
          CreateData();
      }
  
      private void CreateData()
      {
          _service.CreateTestCycle();
      }
  }
  
  
  public class CycleService : GenericDataService&lt;Cycle&gt;
  {
      private readonly IUnitOfWork&lt;WpfRadDispenserDbContext&gt; _uowContext;
      
      public CycleService(IUnitOfWork&lt;WpfRadDispenserDbContext&gt; uowContext)
          : base(uowContext)
      {
          _uowContext = uowContext;
      }
  
      public void CreateTestCycle()
      {
          var cycleDataService = new GenericDataService&lt;Cycle&gt;(_uowContext);
          var vialDataService = new GenericDataService&lt;Vial&gt;(_uowContext);
  
          Cycle c = new Cycle();
          c.BatchName = &quot;test&quot;;
  
          Vial v = new Vial();
          v.Name = &quot;Test Vial&quot;;
          c.Vials.Add(v);
  
          _uowContext.CreateTransaction(IsolationLevel.ReadCommitted);
          try
          {
              vialDataService.Create(v);
              _uowContext.Persist();
  
              var list = vialDataService.GetAll();
  
              cycleDataService.Create(c);
              _uowContext.Persist();
              _uowContext.Commit();
          }
          catch (Exception e)
          {
              Console.WriteLine(e);
              _uowContext.RollBack();
              throw;
          }
          finally
          {
              _uowContext.Dispose();
          }
      }
  }
  </code></pre>
  <p>I have implemented the service in that manner because, for now, I don't know the requirement of the service I want to have the possibility to control the transaction on the Business logic and the posibility to mix repository of any entities for any data service.</p>
  <p>But by using this kind of registration on the container</p>
  <pre class="lang-cs prettyprint-override"><code>private static Container Bootstrap()
  {
      // Create the container as usual.
      var container = new Container();
  
  
  
      // Register your types:
      // Register your windows and view models:
  
      container.Register&lt;WpfRadDispenserDbContextFactory&gt;(Lifestyle.Transient);
      container.Register&lt;IUnitOfWork&lt;WpfRadDispenserDbContext&gt;,WpfRadDispenserUOW&gt;();
      container.Register(typeof(CycleService));
  
      container.Register&lt;IWpfRadDispenserViewModelFactory&lt;ProductionViewModel&gt;,
                              ProductionViewModelFactory&gt;(Lifestyle.Transient);
      container.Register&lt;IWpfRadDispenserViewModelFactory&lt;AnagraphicViewModel&gt;, 
                              AnagraphicsViewModelFactory&gt;(Lifestyle.Transient);
  
      container.Register&lt;IWpfRadDispenserViewModelFactory&lt;HeaderviewModel&gt;,
          HeaderViewModelFactory&gt;(Lifestyle.Transient);
  
      container.Register&lt;IViewModelAbstractFactory,
          ViewModelAbstractFactory&gt;(Lifestyle.Transient);
  
      container.Register&lt;INavigator, Navigator&gt;(Lifestyle.Transient);
      container.Register&lt;MainWindowViewModel&gt;();
      container.Register&lt;MainWindow&gt;();
  
      //container.Options.EnableAutoVerification = false;
      //container.Verify();
      return container;
  }
  </code></pre>
  <p>I don't understand why when I call the <code>CycleService</code> the second time I receive this kind of exception:</p>
  <blockquote>
  <p>System.ObjectDisposedException: 'Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'WpfRadDispenserDbContext'.'</p>
  </blockquote>
  <p>I have understand that, with all instance registered as transient, every time a new-ing up an instance all the required instance are new or there is something more?</p>
  <p>Edit: I have modified by adding all the part that call the cycleService.</p>
  <p>Basically what i want is that when a user execute navigation command (The name UpdateHeaderViewModel are not significant for now...it's just an example)
  a new Viewmodel vith it's own view are created along with all the required service...every time with a new instance.</p>
  <p>Ps. With a few modification by introducing this factory i have resolved the problem</p>
  <pre class="lang-cs prettyprint-override"><code>public interface IUnitOfWorkFactory&lt;TContext&gt; where TContext : DbContext
  {
      IUnitOfWork&lt;TContext&gt; CreateUnitOfWork();
  }
  
  public class WpfRadDispenserUOWFactory:IUnitOfWorkFactory&lt;WpfRadDispenserDbContext&gt;
  {
      private readonly WpfRadDispenserDbContextFactory _factory;
  
      public WpfRadDispenserUOWFactory(WpfRadDispenserDbContextFactory _factory)
      {
          this._factory = _factory;
      }
  
      public IUnitOfWork&lt;WpfRadDispenserDbContext&gt; CreateUnitOfWork()
      {
          return new WpfRadDispenserUOW(_factory);
      }
  }
  </code></pre>
  <p>and this registration</p>
  <pre class="lang-cs prettyprint-override"><code>private static Container Bootstrap()
  {
      // Create the container as usual.
      var container = new Container();
  
  
  
      // Register your types:
      // Register your windows and view models:
  
      container.Register&lt;WpfRadDispenserDbContextFactory&gt;(Lifestyle.Transient);
      container.Register&lt;IUnitOfWork&lt;WpfRadDispenserDbContext&gt;,WpfRadDispenserUOW&gt;();
      container.Register&lt;IUnitOfWorkFactory&lt;WpfRadDispenserDbContext&gt;, WpfRadDispenserUOWFactory&gt;();
      container.Register(typeof(ProductionService));
  
      container.Register&lt;IWpfRadDispenserViewModelFactory&lt;ProductionViewModel&gt;,
                              ProductionViewModelFactory&gt;(Lifestyle.Transient);
      container.Register&lt;IWpfRadDispenserViewModelFactory&lt;AnagraphicViewModel&gt;, 
                              AnagraphicsViewModelFactory&gt;(Lifestyle.Transient);
  
      container.Register&lt;IWpfRadDispenserViewModelFactory&lt;HeaderviewModel&gt;,
          HeaderViewModelFactory&gt;(Lifestyle.Transient);
  
      container.Register&lt;IViewModelAbstractFactory,
          ViewModelAbstractFactory&gt;(Lifestyle.Transient);
  
      container.Register&lt;INavigator, Navigator&gt;(Lifestyle.Transient);
      container.Register&lt;MainWindowViewModel&gt;();
      container.Register&lt;MainWindow&gt;();
  
      //container.Options.EnableAutoVerification = false;
      //container.Verify();
      return container;
  }
  
  </code></pre>