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

Custom ActorService can passed to RegisterActor #40

Closed pvivera closed 6 years ago

pvivera commented 6 years ago

close #36

alexmg commented 6 years ago

Thanks for the PR @pvivera.

I'm wondering if instead of using the Activator.CreateInstance call the ActorService type can instead be resolved from the container. That would allow other services to also be injected into it along with the standard set of parameters.

Do you think this additional capability would be useful to you when creating a custom ActorService class? cc: @jefersonmanjos

pvivera commented 6 years ago

@alexmg That was my thought at the first time but then looking the code I saw that is was using new ActorService to create the instance, so I took the decision to use CreateInstance. I've updated the PR to use a registered ActorService that it could be overridden by a custom one.

jefersonmanjos commented 6 years ago

@alexmg this should be useful, but it's necessary allow generic type for actorservice. Because in the same application I need differents actorservices.

This the code that I use today to solve this problems. For each different actorservice I need to create a new function to register. So if we can create a generic way to register actor with actorservices, would be amazing.

` public ServiceFabricStartupConfiguration RegisterActorCustomService() where T : Actor { RegisterType(); actorsRegisters.Add( new Lazy(() => { return ActorRuntime.RegisterActorAsync((context, actorTypeInfo) => {

                    return new CustomActorService(
                        context,
                        actorTypeInfo,
                        (actorService, actorId) =>
                        {
                            var lifetimeScope = container.BeginLifetimeScope(builderLocal =>
                            {
                                builderLocal.RegisterInstance(context)
                                    .As<StatefulServiceContext>()
                                    .As<ServiceContext>();
                                builderLocal.RegisterInstance(actorService)
                                    .As<ActorService>();
                                builderLocal.RegisterInstance(actorId)
                                    .As<ActorId>();
                            });
                            var actorResolve = lifetimeScope.Resolve<T>();
                            return actorResolve;
                        });
                });
            }));

        return this;
    }`
alexmg commented 6 years ago

I haven't forgotten this @pvivera. Hoping to hit this one tomorrow.

alexmg commented 6 years ago

This change is in Autofac.ServiceFabric 2.1.0-develop-00053 on our MyGet feed (https://www.myget.org/F/autofac/api/v3/index.json). It would be awesome if you could give it a test.