Is there any way to inject dependencies in to the Azure Service Fabric Actor's constructor?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Why not just use some root element field in actor, and resolve it from container with injected dependencies in Actor's constructor? If this is a bad decision, please explain why:
Updated
Its all on github and myget now: https://github.com/s-innovations/S-Innovations.ServiceFabric.Unity
and integrates with aspnet core dependency injection without to much hassle, check the examples of the readme.md
I am a long time user of Unity and decided to make the core extension methods needed to have a nice dependency injection experience when working with actors.
My program.cs now looks like this:
where I in actors and services can just put in my dependencies in the constructors.
If you feel this is useful and would like me to put it into a nuget package, upvote this answer.
One note about the implementation, each actor will get its own scope. This means that all dependencies registered with 'HierarchicalLifetimeManager' that implements
IDisposable
will automaticly get disposed in the actorOnDeactivationAsync
. This was done by dynamicly proxying the actor class with a dynamic type that intercepts the call toOnDeactivationAsync
. For this to work the Actor must be public defined.IActorDeactivationInterception.cs
ActorProxyTypeFactory.cs
OnActorDeactivateInterceptor.cs
UnityFabricExtensions.cs
I know this is old but for documentations' sake DI is now supported in the Reliable Actor framework just like you would expect.
And then you register the Actor with its dependency with the Service Fabric like this:
If you're using Autofac, they have a specific integration package for that:
https://alexmg.com/introducing-the-autofac-integration-for-service-fabric/ https://www.nuget.org/packages/Autofac.ServiceFabric/
In short, registration is performed using
ActorRuntime.RegisterActorAsync
/ServiceRuntime.RegisterServiceAsync
as you would expect. However the more problematic part, namely object release, is automatically handled in theOnDeactivateAsync
/OnCloseAsync
/OnAbort
overrides using a dynamic proxy. Proper lifetime scoped is maintained as well.At the time of writing it's still in Alpha though (just released last month).
Having had a bit of a dig-around in this area with dotPeek a while back (trying to resolve actors from an Autofac lifetime scope per-invocation), I think the trick is to create your own implementation of StatelessActorServiceFactory, and your own extension method to register the actor with it. Although the factory class is marked as internal, its interface (IStatelessServiceFactory) and the service type it creates (StatelessActorServiceInstance) are both public. Unfortunately, it doesn't look like StatelessActorServiceInstance was designed to be extensible (I'm hoping this is just an oversight).
Unfortunately, it looks like WcfActorCommunicationProvider is also marked as internal so you'll pretty much have to create your own pipeline from scratch:
Doesn't really seem worth the effort anymore, does it? :-/
That's where I gave up for now. I don't think it's worth trying to roll-your-own for now given the relative immaturity of the public API, since if this sort of functionality will show up at all, they'll probably do so in a way that'll break anything your implement yourself.