Beispiel #1
0
        public void RegisterInfrastructureServiceFactory(IStatefulServiceFactory factory)
        {
            var activationContext = FabricRuntime.GetActivationContext();
            var broker            = new ServiceFactoryBroker(factory, activationContext);

            Utility.WrapNativeSyncInvokeInMTA(() => this.nativeAgent.RegisterInfrastructureServiceFactory(broker), "InfrastructureServiceAgent.RegisterInfrastructureServiceFactory");
        }
Beispiel #2
0
        private NativeRuntime.IFabricServiceGroupFactory CreateServiceGroupFactoryHelper(ServiceGroupFactory factory)
        {
            NativeRuntime.IFabricServiceGroupFactoryBuilder nativeBuilder = this.NativeRuntimeObject.CreateServiceGroupFactoryBuilder();

            foreach (var namedFactory in factory.StatefulServiceFactories)
            {
                ServiceFactoryBroker broker = new ServiceFactoryBroker(namedFactory.Item2, this.codePackageActivationContext);
                using (var pin = new PinBlittable(namedFactory.Item1))
                {
                    nativeBuilder.AddStatefulServiceFactory(pin.AddrOfPinnedObject(), broker as NativeRuntime.IFabricStatefulServiceFactory);
                }
            }

            foreach (var namedFactory in factory.StatelessServiceFactories)
            {
                ServiceFactoryBroker broker = new ServiceFactoryBroker(namedFactory.Item2, this.codePackageActivationContext);
                using (var pin = new PinBlittable(namedFactory.Item1))
                {
                    nativeBuilder.AddStatelessServiceFactory(pin.AddrOfPinnedObject(), broker as NativeRuntime.IFabricStatelessServiceFactory);
                }
            }

            NativeRuntime.IFabricServiceGroupFactory serviceGroupFactory = nativeBuilder.ToServiceGroupFactory();
            return(serviceGroupFactory);
        }
Beispiel #3
0
        /// <summary>
        /// <para>Registers the specified <see cref="System.Fabric.IStatefulServiceFactory" /> for the specified service type with the specified
        /// <paramref name="timeout" /> and <paramref name="cancellationToken" />.</para>
        /// </summary>
        /// <param name="serviceTypeName">
        /// <para>The type name of the service type (as a string).  This should match the type of the service group type as specified in the manifests
        /// and/or the CreateService command.</para>
        /// </param>
        /// <param name="factory">
        /// <para>The <see cref="System.Fabric.IStatefulServiceFactory" /> which can create the specified service type.</para>
        /// </param>
        /// <param name="timeout">
        /// <para>The maximum amount of time Service Fabric will allow this operation to continue before returning a TimeoutException.</para>
        /// </param>
        /// <param name="cancellationToken">
        /// <para>The <see cref="System.Threading.CancellationToken" /> that the operation is observing.  It can be used to send a notification that
        /// the operation should be canceled.  Note that cancellation is advisory and that the operation may still be completed even if it is canceled.</para>
        /// </param>
        /// <returns>
        /// <para>The representing the asynchronous operation.</para>
        /// </returns>
        public Task RegisterStatefulServiceFactoryAsync(string serviceTypeName, IStatefulServiceFactory factory, TimeSpan timeout, CancellationToken cancellationToken)
        {
            this.ThrowIfDisposed();

            FabricRuntime.ValidateParametersForRegisterServiceFactory(serviceTypeName, factory);

            ServiceFactoryBroker broker = new ServiceFactoryBroker(factory, this.codePackageActivationContext);

            return(this.RegisterServiceFactoryAsyncHelper(true, serviceTypeName, broker, (uint)timeout.TotalMilliseconds, cancellationToken));
        }
Beispiel #4
0
        /// <summary>
        /// <para>Asynchronously associates the specified serviceTypeName with the actual managed Type that implements it, with the specified <paramref name="timeout" />
        /// and <paramref name="cancellationToken" /></para>
        /// </summary>
        /// <param name="serviceTypeName">
        /// <para>The type name of the service type (as a string).  This should match the type of the service group type as specified in the manifests
        /// and/or the CreateService command.</para>
        /// </param>
        /// <param name="serviceTypeImplementation">
        /// <para>The qualified service Type that implements the specified <paramref name="serviceTypeName" />.</para>
        /// </param>
        /// <param name="timeout">
        /// <para>The maximum amount of time Service Fabric will allow this operation to continue before returning a TimeoutException.</para>
        /// </param>
        /// <param name="cancellationToken">
        /// <para>The <see cref="System.Threading.CancellationToken" /> that the operation is observing.  It can be used to send a notification that the
        /// operation should be canceled.  Note that cancellation is advisory and that the operation may still be completed even if it is canceled.</para>
        /// </param>
        /// <returns>
        /// <para>The task representing the asynchronous operation.</para>
        /// </returns>
        public Task RegisterServiceTypeAsync(string serviceTypeName, Type serviceTypeImplementation, TimeSpan timeout, CancellationToken cancellationToken)
        {
            this.ThrowIfDisposed();

            bool isStateful = FabricRuntime.ValidateParametersForRegisterServiceType(serviceTypeName, serviceTypeImplementation);

            ServiceFactoryBroker factory = new ServiceFactoryBroker(new DefaultServiceFactory(serviceTypeImplementation), this.codePackageActivationContext);

            return(this.RegisterServiceFactoryAsyncHelper(isStateful, serviceTypeName, factory, (uint)timeout.TotalMilliseconds, cancellationToken));
        }
        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);
        }
Beispiel #6
0
 private void InternalRegisterServiceFactory(bool isStateful, string serviceType, ServiceFactoryBroker serviceFactoryBroker)
 {
     //// Calls native code, requires UnmanagedCode permission
     using (var pin = new PinBlittable(serviceType))
     {
         if (!isStateful)
         {
             this.NativeRuntimeObject.RegisterStatelessServiceFactory(pin.AddrOfPinnedObject(), serviceFactoryBroker);
         }
         else
         {
             this.NativeRuntimeObject.RegisterStatefulServiceFactory(pin.AddrOfPinnedObject(), serviceFactoryBroker);
         }
     }
 }
Beispiel #7
0
        private void RegisterServiceFactoryHelper(bool isStateful, string serviceTypeName, object factory)
        {
            ServiceFactoryBroker broker = new ServiceFactoryBroker(factory, this.codePackageActivationContext);

            this.InternalRegisterServiceFactory(isStateful, serviceTypeName, broker);
        }
Beispiel #8
0
        private void RegisterServiceTypeHelper(bool isStateful, string serviceTypeName, Type serviceTypeImplementation)
        {
            ServiceFactoryBroker factory = new ServiceFactoryBroker(new DefaultServiceFactory(serviceTypeImplementation), this.codePackageActivationContext);

            this.InternalRegisterServiceFactory(isStateful, serviceTypeName, factory);
        }