private FabricReplicator CreateFabricReplicatorHelper(IStateProvider stateProvider, ReplicatorSettings replicatorSettings)
        {
            Requires.Argument <IStateProvider>("stateProvider", stateProvider).NotNull();

            // Initialize replicator
            if (replicatorSettings == null)
            {
                AppTrace.TraceSource.WriteNoise("StatefulPartition.CreateFabricReplicator", "Using default replicator settings");
            }
            else
            {
                AppTrace.TraceSource.WriteNoise("StatefulPartition.CreateFabricReplicator", "{0}", replicatorSettings);
            }

            NativeRuntime.IFabricReplicator      nativeReplicator;
            NativeRuntime.IFabricStateReplicator nativeStateReplicator;

            StateProviderBroker stateProviderBroker = StatefulServicePartition.CreateStateProviderBroker(stateProvider);

            using (var pin = new PinCollection())
            {
                unsafe
                {
                    if (replicatorSettings == null)
                    {
                        nativeStateReplicator = this.nativePartition.CreateReplicator(stateProviderBroker, IntPtr.Zero, out nativeReplicator);
                    }
                    else
                    {
                        IntPtr nativeReplicatorSettings = replicatorSettings.ToNative(pin);
                        nativeStateReplicator = this.nativePartition.CreateReplicator(stateProviderBroker, nativeReplicatorSettings, out nativeReplicator);
                    }
                }
            }

            NativeRuntime.IOperationDataFactory nativeOperationDataFactory = (NativeRuntime.IOperationDataFactory)nativeStateReplicator;
            if (nativeStateReplicator == null || nativeReplicator == null || nativeOperationDataFactory == null)
            {
                AppTrace.TraceSource.WriteError("StatefulPartition.Create", "Native replicators could not be initialized.");
                throw new InvalidOperationException(StringResources.Error_StatefulServicePartition_Native_Replicator_Init_Failed);
            }

            stateProviderBroker.OperationDataFactory = new OperationDataFactoryWrapper(nativeOperationDataFactory);

            return(new FabricReplicator(nativeReplicator, nativeStateReplicator, nativeOperationDataFactory));
        }
        private Task <IReplicator> OpenAsync(
            NativeTypes.FABRIC_REPLICA_OPEN_MODE openMode,
            NativeRuntime.IFabricStatefulServicePartition partition,
            CancellationToken cancellationToken)
        {
            AppTrace.TraceSource.WriteNoise(
                "StatefulServiceReplicaBroker.OpenAsync",
                "OpenAsync for ServiceName {0}. Uri {1}. ReplicaId {2}. PartitionId {3}, OpenMode {4}",
                this.initializationParameters.ServiceTypeName,
                this.initializationParameters.ServiceName,
                this.replicaId,
                this.initializationParameters.PartitionId,
                openMode);

            this.statefulPartition = this.CreateStatefulPartition(partition);

            return(this.statefulServiceReplica.OpenAsync((ReplicaOpenMode)openMode, this.statefulPartition, cancellationToken));
        }