autofac / Autofac.ServiceFabric

Autofac integration for Azure Service Fabric. Provides service factory implementations for Actors, Stateful Services and Stateless Services.
MIT License
26 stars 27 forks source link

Instanceperrequest equivalent Scope in Statefulservice #23

Closed saisworld closed 6 years ago

saisworld commented 6 years ago

I have an exising web app that I'm migrating into SeviceFabric and got struck at this a strange problem. Is there an equivalent scope of Instanceperrequest in stateful service ? I have few objects that only needs to be instantiated once per request via ServiceRemoting to my stateful service. However, I cant find it anything that I could use in Servicefabric, please advise.

saisworld commented 6 years ago

I'm facing same problem while using Actors. I have a DBContext instance and its registered to container using InstancePerdenpendency and when I create multiple actors during runtime, I get the error - "ExceptionMessage":"The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe."

So looks like same instance of DbContext object is being served by Autofac container to multiple threads as dbcontext class is not thread safe we are having this problem. Is there a lifetimescope that is equivalent to InstancePerRequest for ServiceFabric as well?

alexmg commented 6 years ago

It is possible to achieve "per request" scoping by using the various relationship types that are part of core Autofac. Have a read through issue #4 for the details and some example code. 😃

tillig commented 6 years ago

I added some quick docs around the Service Fabric integration which includes this faux per-request info so hopefully it'll help folks in the future.