title: Autofac SingleInstance() and Xamarin Forms
body: <p>To start, let me say that I have read several questions here about SingleInstance, but still cannot find a direct answer that helps me. That said, I apologize if I missed anything.</p>
  
  <p>Here's my question:</p>
  
  <p>I am building a Xamarin Forms app for iOS and Android. I have a single AppInitializer class in a PCL where I register all of my interface dependencies using Autofac.  I then assign the Container from the builder as a static property on the app class. The problem I encounter is that while I'm registering everything with .SingleInstance(), I'm not actually getting a single instance.</p>
  
  <p><strong>Init Logic Example:</strong></p>
  
  <pre><code>var builder = new ContainerBuilder();
  builder.RegisterType&lt;ErrorHandler&gt;().SingleInstance().As&lt;IErrorHandler&gt;();
  …
  builder.RegisterType&lt;MemberViewModel&gt;().SingleInstance().As&lt;IMemberViewModel&gt;();
  …
  AppContainer.Current = builder.Build();
  </code></pre>
  
  <p>I am letting Autofac handle resolving interfaces in my constructors.  For example:</p>
  
  <pre><code>public MemberViewModel(ISettingsViewModel settings, IErrorHandler errorHandler, …) : base(settings, errorHandler){…}
  </code></pre>
  
  <p>I then use said model on a page as below:</p>
  
  <p><strong>Example page usage:</strong></p>
  
  <pre><code>public ProfilePage()
  {
       InitializeComponent();
       var displayModel = Model.CurrentMember;
       …
   }
   …
   **public IMemberViewModel Model =&gt;
   AppContainer.Current.Resolve&lt;IMemberViewModel&gt;();**
  </code></pre>
  
  <p>In this example I set Model.CurrentMember's properties immediately before arriving on this page. I've set breakpoints and know for a fact this is happening.  However, when I resolve the instance of the model, the properties on CurrentMember are null.</p>
  
  <p>Am I doing something wrong here or have I encountered a bug?</p>
  
  <p><strong>-Edit-</strong> 
  Made it clear that I'm using Autofac.</p>
  
  <p><strong>-Edit 2-</strong>
  Adding more detail.</p>
  
  <p>My implementation of the IMemberViewModel class has various properties on it, including an observable object called current member.  It is declared as below:</p>
  
  <pre><code> public class MemberViewModel : ViewModelBase, IMemberViewModel
   {
    … 
    (see constructor above)
    …
    public MemberDisplay CurrentMember =&gt;
          m_CurrentMember ?? (m_CurrentMember = new MemberDisplay())
  </code></pre>
  
  <p>On the implementation of IMemberViewModel I have a method that sets the various properties on CurrentMember.</p>
  
  <p>The order of operations is this:</p>
  
  <p>The end user taps an image for a member. This fires a command on the (theoretically) singleton instance of the IMemberViewModel implementation. This command executes an async task that awaits an async call to the API to load the data for that member.  After that data is loaded and the properties set on CurrentMember, the app navigates to the profile screen.  The profile screen resolves IMemberViewModel (per above). </p>
  
  <p><strong>Expected Behavior:</strong>
  The properties on CurrentMember from the resolved instance of IMemberViewModel are set to the values that have just been set from the load data method.  This expectation arises from assuming that there is a single instance of IMemberViewModel.</p>
  
  <p><strong>Actual Behavior:</strong>
  The CurrentMember's properties are at their default values, i.e. string.Empty, 0, null, etc.</p>
  
  <p>The odd thing here is that this doesn't happen to every model.  I have a message model that I am resolving in the same manner on the same screen and it seems to be fine.</p>