title: MVVMLight Simple IOC - dynamically register and unregister data service implementations
body: <p>I have a WPF application with MVVM Light Toolkit support.  The application has the following scenario.
  The application has two Data connection modes. One is <strong>WCF service</strong> and the other is <strong>Direct Database</strong>. The application should connect to the Database through one of the above mentioned modes. The connection mode selection is situated in Login window. The end user can select one of the connection modes (WCF Service or Direct Database) and based on that selection a list of connection configurations are loaded in Combo box. (Please check the attached image for more information). The connection configurations are located in a local xml configuration file. Also I have a default connection configuration which should be assigned if any of the connection configurations is selected.</p>
  
  <p><img src="https://i.stack.imgur.com/5TGDZ.png" alt="enter image description here"></p>
  
  <p>In the View Model Locator, I am registering the default service as follows</p>
  
  <pre><code>public ViewModelLocator()
          {
              ServiceLocator.SetLocatorProvider(() =&gt; SimpleIoc.Default);
  
              if (ViewModelBase.IsInDesignModeStatic)
              {
                  // Create design time view services and models
                  if (!SimpleIoc.Default.IsRegistered&lt;IDataService&gt;())
                      SimpleIoc.Default.Register&lt;IDataService, MockDataClient&gt;();
              }
              else
              {
                  // Create run time view services and models
                  if (!SimpleIoc.Default.IsRegistered&lt;IDataService&gt;())
                  {
                      switch (DefaultConnectionConfiguration.ConnectionMode)
                      {
                          case DataConnectionMode.WcfService:
                              var wcfServiceConfiguration = (WcfServiceConfiguration)CurrentConnectionConfiguration;
                              SimpleIoc.Default.Register&lt;IDataService&gt;(
                                  () =&gt;
                                  wcfServiceConfiguration != null
                                      ? new DataServiceClient("WSHttpBinding_IDataService",
                                                                   wcfServiceConfiguration.EndpointUrl)
                                      : null);
                              break;
  
                          case DataConnectionMode.Database:
                              SimpleIoc.Default.Register&lt;IDataService, DbClient&gt;();
                              break;
                      }
                  }
  
              }
          SimpleIoc.Default.Register&lt;LoginViewModel&gt;();
          SimpleIoc.Default.Register&lt;ManageConfigurationsViewModel&gt;();
  </code></pre>
  
  <p>Both <strong>DbClient and DataServiceClient implement IDataservice</strong>.</p>
  
  <p>If there is already a default connection specified in the configuration file, the above code works fine when the view model locator registers the view models at the application start.ie IDataservice is registered with the default connection configuration.</p>
  
  <p>Now the real issue is when the user selects a connection configuration, that connection configuration becomes the default one and i want the MVVM Light to unregister the previous data service and register the newly selected one, and use it to connect to the data.</p>
  
  <p>I tried the following code in the sign in button click and it failed :(</p>
  
  <pre><code>void SignInButtonClick()
  {
          if(SimpleIoc.Default.IsRegistered&lt;IDataService&gt;())
                                      SimpleIoc.Default.Unregister&lt;IDataService&gt;();
  
      switch (DefaultConnectionConfiguration.ConnectionMode)
                              {
                                  case DataConnectionMode.WcfService:
                                      var wcfServiceConfiguration = (WcfServiceConfiguration)CurrentConnectionConfiguration;
                                      SimpleIoc.Default.Register&lt;IDataService&gt;(
                                          () =&gt;
                                          wcfServiceConfiguration != null
                                              ? new DataServiceClient("WSHttpBinding_IDataService",
                                                                           wcfServiceConfiguration.EndpointUrl)
                                              : null);
                                      break;
  
                                  case DataConnectionMode.Database:
                                      SimpleIoc.Default.Register&lt;IDataService, DbClient&gt;();
                                      break;
                              }
  //perform authentication process
  }
  </code></pre>
  
  <p><strong>Updated Code</strong></p>
  
  <pre><code>public class ViewModelLocator
      {
          public ViewModelLocator()
          {
              ServiceLocator.SetLocatorProvider(() =&gt; SimpleIoc.Default);
  
              if (ViewModelBase.IsInDesignModeStatic)
              {
                  // Create design time view services and models
                  if (!SimpleIoc.Default.IsRegistered&lt;IDataService&gt;())
                      SimpleIoc.Default.Register&lt;IDataService, MockDataClient&gt;();
              }
              SimpleIoc.Default.Register&lt;LoginViewModel&gt;();
          }
  
          public LoginViewModel LoginViewModel
          {
              get
              {
                  return ServiceLocator.Current.GetInstance&lt;LoginViewModel&gt;();
              }
          }
  
          public static void Cleanup()
          {
              // TODO Clear the ViewModels
              ServiceLocator.Current.GetInstance&lt;LoginViewModel&gt;().Cleanup();
          }
      }
  
  
  
  public class LoginViewModel : ViewModelBase
      {
          ICometDataService service;
  
      #region Constructor
          public LoginViewModel()
          {
  
          }
          public LoginViewModel(IDataService dataService)
              : base(dataService)
          {
              service = dataService;
          }
  
          #endregion
  }
  </code></pre>