microsoft / ApplicationInsights-ServiceFabric

ApplicationInsights SDK for ServiceFabric projects
MIT License
63 stars 26 forks source link

MessageHandler for Actors? #24

Closed aloneguid closed 7 years ago

aloneguid commented 7 years ago

Brilliant piece of code, thank you. I can't figure out though how to initialise message handler for Actors on server side. The client is passing the headers, however where do I place the code in Actor implementation to pick them up similar to CreateServiceInstanceListeners() in reliable services?

yantang-msft commented 7 years ago

You can override the CreateServiceReplicaListeners() method of ActorService to add the message handler like this

class MyActorService : ActorService
{
    public MyActorService(
        StatefulServiceContext context,
        ActorTypeInformation actorTypeInfo,
        Func<ActorService, ActorId, ActorBase> actorFactory = null,
        Func<ActorBase, IActorStateProvider, IActorStateManager> stateManagerFactory = null,
        IActorStateProvider stateProvider = null,
        ActorServiceSettings settings = null)
    : base(context, actorTypeInfo, actorFactory, stateManagerFactory, stateProvider, settings)
    {
    }

    protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
    {
        return new[]
        {
            new ServiceReplicaListener(
                context => new FabricTransportActorServiceRemotingListener(
                    context,
                    new CorrelatingRemotingMessageHandler(this),
                    new FabricTransportRemotingListenerSettings()))
        };
    }
}

And host your actor using this service

ActorRuntime.RegisterActorAsync<SomeActor>(
                    (context, actorType) => new MyActorService(context, actorType)).GetAwaiter().GetResult();
aloneguid commented 7 years ago

works like a charm, thank you!