title: autofac and owned instances
body: <p>I have been reasing about owned instances and I have a need to set one up.
  <a href="http://docs.autofac.org/en/latest/advanced/owned-instances.html" rel="nofollow noreferrer">http://docs.autofac.org/en/latest/advanced/owned-instances.html</a></p>
  
  <p>I have 2 methods in the same class that use this owned instance. I have set it up like this:</p>
  
  <pre><code>private readonly Func&lt;SessionDetails, Owned&lt;ITroposUnitOfWork&gt;&gt; _paramatizedTroposUnitOfWork;
  </code></pre>
  
  <p>My unit of work class constructor looks like this:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Used for creating manual sessions
  /// &lt;/summary&gt;
  /// &lt;param name="sessionDetails"&gt;The details of the session to be created&lt;/param&gt;
  public TroposUnitOfWork(SessionDetails sessionDetails)
  {
  
      // Throw if we don't supply any details
      ThrowIf.ArgumentIsNull(() =&gt; sessionDetails);
  
      // Start the session
      StartSession(sessionDetails);
  }
  </code></pre>
  
  <p>So, my understanding is that if I use a <strong>using</strong> block, then the unit of work will be disposed of at the end of the call. But it isn't.
  Like I mentioned before, I have 2 methods that use this owned instance. They are:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Creates the Tropos user
  /// &lt;/summary&gt;
  /// &lt;param name="model"&gt;The user to be created&lt;/param&gt;
  /// &lt;param name="password"&gt;The password to set&lt;/param&gt;
  private async Task CreateTroposUserAsync(User model, string password)
  {
  
      // If there is no password, throw an error
      ThrowIf.ArgumentIsNull(() =&gt; password);
  
      // Get our master user
      var user = await base.FindByNameAsync(model.Master);
  
      // If we have no master user, throw an error
      if (user == null) throw new ObjectNotFoundException();
  
      // Create our session details
      var sessionDetails = _troposSession.Value.Create(user);
  
      // User our new user
      using (var troposUnitOfWork = _paramatizedTroposUnitOfWork(sessionDetails))
      {
  
          try
          {
  
              // Create our tropos user service
              var userService = new TroposUserService(troposUnitOfWork.Value);
  
              // Create our user
              var transaction = userService.Create(model);
  
              // Save our changes (Don't throw an error if the user already exists)
              troposUnitOfWork.Value.RunTransaction(transaction);
  
          } catch (Exception ex)
          {
  
              // Display human readable messages
              throw new Exception(ex.Message);
          }
      }
  
      // Sets the new users password
      SetTroposPassword(model, password);
  
      // Update the flag
      model.HasTroposLogin = true;
  }
  </code></pre>
  
  <p>And the other one is: </p>
  
  <pre><code>/// &lt;summary&gt;
  /// Sets the tropos password
  /// &lt;/summary&gt;
  /// &lt;param name="model"&gt;The user that needs the password setting&lt;/param&gt;
  /// &lt;param name="password"&gt;&lt;/param&gt;
  private void SetTroposPassword(User model, string password)
  {
  
      // Create our session details
      var sessionDetails = _troposSession.Value.Create(model.UserName);
  
      // Create our anonymous session
      using (var troposUnitOfWork = _paramatizedTroposUnitOfWork(sessionDetails))
      {
  
          // Create our tropos user service
          var userService = new TroposUserService(troposUnitOfWork.Value);
  
          // Set our password
          var transaction = userService.ChangePassword(password);
  
          // Save our changes
          troposUnitOfWork.Value.RunTransaction(transaction);
      }
  }
  </code></pre>
  
  <p>The first method does invoke the second method, but outside the <strong>using</strong> block. I put a breakpoint in the <strong>TroposUnitOfWork</strong> dispose method and it only gets hit once. The constructor is only hit once too.
  Does anyone know why?</p>