예제 #1
0
        internal StatelessServiceBroker(IStatelessServiceInstance statelessServiceInstance, ServiceInitializationParameters initializationParameters, long instanceId)
        {
            Requires.Argument("statelessServiceInstance", statelessServiceInstance).NotNull();
            Requires.Argument("initializationParameters", initializationParameters).NotNull();

            this.statelessService         = statelessServiceInstance;
            this.initializationParameters = initializationParameters;
            this.instanceId = instanceId;
        }
        public StatefulServiceReplicaBroker(IStatefulServiceReplica statefulServiceReplica, ServiceInitializationParameters initializationParameters, long replicaId)
        {
            Requires.Argument("statefulServiceReplica", statefulServiceReplica).NotNull();
            Requires.Argument("initializationParameters", initializationParameters).NotNull();

            this.statefulServiceReplica   = statefulServiceReplica;
            this.initializationParameters = initializationParameters;
            this.replicaId = replicaId;

            // KVS is currently the only service that internally supports returning additional
            // DeployedStatefulServiceReplicaDetail information. If we ever support custom
            // information from user services, then GetQueryResult() can be exposed via IStatefulServiceReplica.
            //
            var casted = this.statefulServiceReplica as KeyValueStoreReplica;

            if (casted != null)
            {
                this.internalReplica = casted.InternalStore;
            }
        }
예제 #3
0
        private TReturnValue CreateHelper <TFactory, TReturnValue>(
            IntPtr nativeServiceType,
            IntPtr nativeServiceName,
            uint initializationDataLength,
            IntPtr nativeInitializationData,
            Guid partitionId,
            Func <TFactory, ServiceInitializationParameters, TReturnValue> creationFunc,
            Action <TReturnValue, ServiceInitializationParameters> initializationFunc,
            ServiceInitializationParameters initializationParameters)
            where TReturnValue : class
            where TFactory : class
        {
            ServiceFactoryBroker.ValidatePublicApiArguments(nativeServiceType, nativeServiceName);

            string managedServiceType = NativeTypes.FromNativeString(nativeServiceType);
            string managedServiceName = NativeTypes.FromNativeString(nativeServiceName);

            byte[] initializationData = ServiceFactoryBroker.ParseInitializationData(initializationDataLength, nativeInitializationData);

            Uri serviceNameUri = null;

            if (managedServiceName.StartsWith("fabric:", StringComparison.Ordinal))
            {
                serviceNameUri = new Uri(managedServiceName);

                AppTrace.TraceSource.WriteNoise(
                    "ServiceFactoryBroker.CreateInstance",
                    "Creating Instance for {0} Uri {1} partitionId {2}",
                    managedServiceType,
                    serviceNameUri.OriginalString,
                    partitionId);
            }
            else
            {
                AppTrace.TraceSource.WriteNoise(
                    "ServiceFactoryBroker.CreateInstance",
                    "Creating Instance for {0} System Service {1} partitionId {2}",
                    managedServiceType,
                    managedServiceName,
                    partitionId);
            }

            TFactory factory = this.serviceFactory as TFactory;

            if (factory == null)
            {
                AppTrace.TraceSource.WriteError("ServiceFactoryBroker.CreateInstanceHelper", "ServiceFactory type is incorrect for creation call {0}", this.serviceFactory.GetType().FullName);
                throw new InvalidOperationException(StringResources.Error_ServiceFactoryBroker_Invalid_ServiceFactoryType);
            }

            initializationParameters.InitializationData = initializationData;
            initializationParameters.ServiceTypeName    = managedServiceType;
            initializationParameters.ServiceName        = serviceNameUri;
            initializationParameters.PartitionId        = partitionId;

            TReturnValue instance;

            try
            {
                instance = creationFunc(factory, initializationParameters);
            }
            catch (Exception e)
            {
                AppTrace.TraceSource.WriteExceptionAsWarning("ServiceFactoryBroker.CreateInstanceHelper", e, "the passed in servicefactory threw exception");
                CreateReplicaOrInstanceApi.HandleException(e);
                throw;
            }

            if (instance == null)
            {
                AppTrace.TraceSource.WriteError("ServiceFactoryBroker.CreateInstance", "ServiceFactory returned null {0}", this.serviceFactory.GetType().FullName);
                throw new InvalidOperationException(StringResources.Error_ServiceFactoryBroker_Null_Return);
            }

            try
            {
                initializationFunc(instance, initializationParameters);
            }
            catch (Exception e)
            {
                AppTrace.TraceSource.WriteExceptionAsWarning("ServiceFactoryBroker.CreateInstanceHelper", e, "the service threw an exception in Initialize");
                CreateReplicaOrInstanceApi.HandleException(e);
                throw;
            }

            return(instance as TReturnValue);
        }