Recommended way to register custom serializer with StateManager

偶尔善良 提交于 2019-12-12 18:16:56

问题


In the pre-GA version of Service Fabric I was registering a custom serializer like this:

    protected override IReliableStateManager CreateReliableStateManager()
    {
        IReliableStateManager result = new ReliableStateManager(
            new ReliableStateManagerConfiguration(
                onInitializeStateSerializersEvent: InitializeStateSerializers));

        return result;
    }

    private Task InitializeStateSerializers()
    {
        StateManager.TryAddStateSerializer(new KFOBinarySerializer());
        return Task.FromResult(false);
    }

However, the CreateReliableStateManager method was removed in the GA version. I've struggled to get something working in its place. Currently I'm calling

StateManager.TryAddStateSerializer(new KFOBinarySerializer());

from within the service's RunAsync method, which appears to work fine.

  1. What is the recommended way to register a custom serializer?
  2. TryAddStateSerializer is deprecated. Anyone know if this is because custom serialization support will go away or if it will simply be supported through some other mechanism?

回答1:


You can create the state manager in the StatefulService's constructor (full example here):

class MyService : StatefulService
{
   public MyService(StatefulServiceContext serviceContext) 
       : base(serviceContext, CreateReliableStateManager()) { }

    private static IReliableStateManager CreateReliableStateManager() { ... }
}

Regarding the deprecated API, Microsoft says it's safe to use, but it will change in the future.



来源:https://stackoverflow.com/questions/36784086/recommended-way-to-register-custom-serializer-with-statemanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!