Comments (6)
Yes, in the case of a stateless service its lifetime is essentially a singleton. The primary reason for supporting these in the integration is to provide a consistent pattern for working with DI in all types of SF services. You can also use the more natural IDisposable
pattern and have the container dispose the appropriate services for you when the service is deactivated.
If you want to have dependencies that should exist only for the duration of a method invocation you can achieve this using an auto-generated factory (Func<T>
). In the case that you need to control disposal of this dependency you can also combine that with an owned dependency (Func<Owned<T>>
) and dispose the instance of T
immediately after you have finished with it in the method. You can find out more about these relationship types on our documentation site.
http://docs.autofac.org/en/latest/resolve/relationships.html
In the example you provided a basic auto-generated factory approach would look something like this:
public class UserService : IUserService
{
private readonly Func<IUserStore> _userStoreFactory;
public UserService(Func<IUserStore> userStoreFactory)
{
_userStoreFactory = userStoreFactory;
}
public async Task<string> GetNameAsync(int id)
{
return await _userStoreFactory().GetNameAsync(id);
}
}
If the IUserStore
implemented IDisposable
and needed to release resources eagerly it would look more like this:
public class UserService : IUserService
{
private readonly Func<Owned<IUserStore>> _userStoreFactory;
public UserService(Func<Owned<IUserStore>> userStoreFactory)
{
_userStoreFactory = userStoreFactory;
}
public async Task<string> GetNameAsync(int id)
{
using (var userStore = _userStoreFactory().Value)
{
return await userStore.GetNameAsync(id);
}
}
}
I would also configure and build your container outside of the factory delegate so you donβt accidentally apply the same pattern when working with other types of services (such as reliable actors) and end up creating multiple instances of the container. When you use the integration all your services (including the SF services) are registered up front before building the container so this wouldn't be an issue.
Hope that helps.
from autofac.servicefabric.
Glad it is working well for you. Yes, the store and its dependencies will be created in their own lifetime scope that will be disposed at the end of the using, essentially making it instance per method call.
from autofac.servicefabric.
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.
from autofac.servicefabric.
For information: microsoft/service-fabric-issues#269 (comment)
from autofac.servicefabric.
Absolutely perfect, thanks so much! This appears to be exactly what I was looking for. I had no idea AutoFac was this powerful. What a great piece of kit! I shall have a play with this in the coming days
from autofac.servicefabric.
I've now implemented this and it works brilliantly.
I opted for
public class UserService : IUserService
{
private readonly Func<Owned<IUserStore>> _userStoreFactory;
public UserService(Func<Owned<IUserStore>> userStoreFactory)
{
_userStoreFactory = userStoreFactory;
}
public async Task<string> GetNameAsync(int id)
{
using (var userStore = _userStoreFactory())
{
return await userStore.Value.GetNameAsync(id);
}
}
}
This way, correct me if I'm wrong, a new scope is created per method call. So any services which are registered as per scope (or transient) will be constructed and then destroyed at the end of this call - In this case the IUserDbContext
. Seems to work as I wanted anyway, unless this is not a sensible thing to do?
from autofac.servicefabric.
Related Issues (20)
- InstancePerMatchingLifetimeScope HOT 2
- Reduced extensibility due to access restrictions in classes and methods HOT 3
- Allow logging exceptions during service instantiation HOT 8
- Access denied in RegisterActor HOT 5
- How to communicate to stateful service use remoting? HOT 2
- How to inject custom ActorService HOT 2
- How to use Serilog, Autofac and Service Fabric Actors together HOT 3
- Dynamic Proxy breaks optional constructor arguments
- Cannot create Stateless Service in Service Fabric injecting my own objects in HOT 2
- Service class constructor never invoked
- Lifetime scope for Actor instance HOT 2
- Not support 6.3.187 HOT 1
- Allow use of current ILifetimeScope in the exception callback HOT 1
- ServiceFabricModule is internal, visible to Autofac RegisterAssemblyModules, but cannot be constructed HOT 8
- Message "Access is denied
- Cannot resolve parameter 'System.Fabric.StatefulServiceContext serviceContext' HOT 2
- Singleton does not play nicely with ServiceContext dependency HOT 1
- MissingMethodException after update to Autofac 5.0.0 HOT 1
- Compatibility with Autofac v6 HOT 1
- Allow injecting additional registrations during the creation of service lifetimescope HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from autofac.servicefabric.