title: UnitOfWork struggling with multiple simultaneous connections
body: <p>Apparently (and quite possibly) there's a flaw in my current UnitOfWork implementation, because I have connection errors when doing many calls at once.</p>
  
  <p><strong><em>Exception:</em></strong></p>
  
  <blockquote>
    <p>The underlying provider failed on Open.</p>
  </blockquote>
  
  <p><strong><em>Inner Exception:</em></strong></p>
  
  <blockquote>
    <p>The connection was not closed. The connection's current state is
    connecting.</p>
    
    <p>This results in a HTTP 500 response on the client side.</p>
  </blockquote>
  
  <p><strong>UnitOfWork implementation</strong></p>
  
  <pre><code>public class ScopedUnitOfWork : IUnitOfWork
  {
      public Entities Context { get; set; }
      public UnitOfWorkState State { get; set; }
  
      public ScopedUnitOfWork(IEnvironmentInformationProvider environmentInformationProvider)
      {
          this.Context = new Entities(environmentInformationProvider.ConnectionString);
          this.State = UnitOfWorkState.Initialized;
      }
  
      public UowScope GetScope()
      {
          this.State = UnitOfWorkState.Working;
  
          return new UowScope(this);
      }
  
      public SaveResult Save()
      {
          if (this.State != UnitOfWorkState.Working)
              throw new InvalidOperationException("Not allowed to save out of Scope. Request an UowScope instance by calling method GetScope().");
  
          this.Context.SaveChanges();
  
          this.State = UnitOfWorkState.Finished;
  
          return new SaveResult(ResultCodes.Ok);
      }
  }
  </code></pre>
  
  <p>Working on a single <code>UowScope</code> would solve the issue but that's not possible given the current circumstance, because each request is completely separate. De facto each request IS using an <code>UoWScope</code>, but apparently it goes wrong when the UoW receives many calls at once.</p>
  
  <p>The UoW is injected through Unity IoC, so I suppose it's a singleton in effect.</p>
  
  <p><strong>The question</strong></p>
  
  <p>Is there a way to adapt the UoW so that separate high-frequency requests are not an issue?</p>
  
  <p>Preferably I'd solve this server side, not client side, any tips? Thanks!</p>
  
  <p><strong>Disclaimer</strong></p>
  
  <p>I don't claim I fully understand UoW, so my implementation may need improvement, be gentle :). Any improvements on that are certainly welcome!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I -know- the EF Context is an UoW, I use mine at Domain level to enable transactional processing of data that is functionality related. And it's also by customer demand, I have no choice.</p>