entity framework - The operation cannot be completed because the DbContext has been disposed - LifestylePerWcfOperation -


i have wcf web service encountering concurrency issues. load small @ moment, expected increase great amount in next few days.

the overall setup wcf, entity framework 6, dependency injection (castle windsor), unitofwork & repository pattern.

i setup stress test hits service in parallel , can recreate concurrency errors such as....

  • an error reported while committing database transaction not determined whether transaction succeeded or failed on database server.
  • the changes database committed successfully, error occurred while updating object context. objectcontext might in inconsistent state. inner exception message: acceptchanges cannot continue because object's key values conflict object in objectstatemanager. make sure key values unique before calling acceptchanges.

  • the property 'id' part of object's key information , cannot modified.

from research i've done, looks should injecting dbcontext lifestyleperwcfoperation. it's being done lifestyletransient.

when switch on lifestyleperwcfoperation , rerun test, every time:

  • the operation cannot completed because dbcontext has been disposed

so have 2 questions:

  1. am correct in assumption true solution switching dbcontext lifestyleperwcfoperation?
  2. if so, apparent why dbcontext getting gobbled up?

here's code:

appstart.cs:

ioccontainer.addfacility<typedfactoryfacility>();  ioccontainer.register(component.for<irepositoryfactory>().asfactory());  ioccontainer.register(component.for<iunitofwork>()                                .implementedby<unitofwork>)                              /*.lifestyleperwcfoperation()*/);  ioccontainer.register(component.for(typeof(idbcontext))                                .implementedby(dbcontexttype)                                .dependson(dependency.onvalue("connectionstring", string.format(configurationmanager.connectionstrings["---"].connectionstring))));                              /*.lifestyleperwcfoperation()*/  ioccontainer.register(component.for(typeof(irepository<>))                                .implementedby(typeof(repository<>))                                .lifestyletransient()                              /*.lifestyleperwcfoperation()*/);  // register actual dbcontexttype consuming applications can access without interface ioccontainer.register(component.for(dbcontexttype)                                .named(dbcontexttype.name)                                .dependson(dependency.onvalue("connectionstring", string.format(configurationmanager.connectionstrings["----"].connectionstring)));                              /*.lifestyleperwcfoperation()*/ 

unitofwork.cs:

public class unitofwork : iunitofwork {     private readonly idbcontext _dbcontext;     private bool _disposed;      public unitofwork(idbcontext dbcontext)     {         _dbcontext = dbcontext;     }      public int commit()     {         return _dbcontext.savechanges();     }      public void dispose()     {         dispose(true);         gc.suppressfinalize(this);     }      protected virtual void dispose(bool disposing)     {         if (!_disposed)         {             if (disposing)             {                 _dbcontext.dispose();             }              _disposed = true;         }     } } 

repository.cs

public class repository<t> : irepository<t> t : class {     private readonly idbset<t> _dbset;      public repository(idbcontext dbcontext)     {         _dbset = dbcontext.set<t>();     }      public iqueryable<t> query()     {         return _dbset.asqueryable();     }      public virtual ienumerable<t> getall()     {         return _dbset.asenumerable();     }      public virtual void add(t entity)     {         _dbset.add(entity);     }      public virtual void delete(t entity)     {         _dbset.remove(entity);     }      public virtual void delete(icollection<t> entities)     {         entities.tolist().foreach(delete);     } } 

i can add more code if needed. can provide!!

in answer questions:

1) correct in changing lifestyle lifestyleperwcfoperation

2) disposing dbcontext within unitofwork. dbcontext injected windsor, hence windsor call dispose on it. when using windsor never dispose injected objects, if created object using factory or resolve have release (not dispose) object, windsor can dispose it.

i expect lifestyleperwcfoperations , removing dispose code, code should run fine.

good luck, marwijn.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -