Esempio n. 1
0
        private void SuccessfulAsyncCreateTestHelper(Action <FabricRuntimeFactoryStub> setup, Action exitNotificationCallback, Action <FabricRuntime, FabricRuntimeFactoryStub> validation = null)
        {
            this.SuccessfulCreateTestHelper(
                setup,
                (stub) =>
            {
                // start creating the runtime on a background task
                var createTask = FabricRuntime.CreateAsync(exitNotificationCallback, FabricRuntimeTest.DefaultTimeout, CancellationToken.None);

                // this will call the async version of the api on the stub -> complete that so that it returns
                stub.AsyncCompleteEvent.Set();

                // the task should complete
                Assert.IsTrue(createTask.Wait(this.GetDurationToWaitForTask()));

                return(createTask.Result);
            },
                validation);
        }
Esempio n. 2
0
        public static async Task <RuntimeContext> GetOrCreateAsync(
            TimeSpan timeout,
            CancellationToken cancellationToken)
        {
            // check if the context exist using double lock pattern if so return it.
            // shared context is never set to null, so the following check is safe
            if (SharedContext != null)
            {
                return(SharedContext);
            }

            lock (SharedContextLock)
            {
                if (SharedContext != null)
                {
                    return(SharedContext);
                }
            }

            // shared context does not exist, create it

            ICodePackageActivationContext codePackageContext = null;
            NodeContext   nodeContext;
            FabricRuntime fabricRuntime = null;

            try
            {
                nodeContext = await FabricRuntime.GetNodeContextAsync(timeout, cancellationToken).ConfigureAwait(false);

                codePackageContext = await FabricRuntime.GetActivationContextAsync(timeout, cancellationToken).ConfigureAwait(false);

                fabricRuntime = await FabricRuntime.CreateAsync(timeout, cancellationToken).ConfigureAwait(false);
            }
            catch
            {
                if (fabricRuntime != null)
                {
                    fabricRuntime.Dispose();
                }
                if (codePackageContext != null)
                {
                    codePackageContext.Dispose();
                }

                throw;
            }

            // set the shared context
            lock (SharedContextLock)
            {
                if (SharedContext == null)
                {
                    SharedContext = new RuntimeContext()
                    {
                        Runtime            = fabricRuntime,
                        CodePackageContext = codePackageContext,
                        NodeContext        = nodeContext
                    };
                }
            }

            // dispose the newly created runtime and context if they do not become the shared
            if (!object.ReferenceEquals(SharedContext.Runtime, fabricRuntime))
            {
                fabricRuntime.Dispose();
            }

            if (!object.ReferenceEquals(SharedContext.CodePackageContext, codePackageContext))
            {
                codePackageContext.Dispose();
            }

            return(SharedContext);
        }
Esempio n. 3
0
        public static int Run(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            AdHocHost.Log("Starting");
            AdHocHost.ParseArguments(args);

            AdHocHost.Log("Querying activation context");
            try
            {
                FabricRuntime.GetActivationContext();
                AdHocHost.isActivationContextAvailable = true;
                AdHocHost.Log("ActivationContext get success");
            }
            catch (InvalidOperationException)
            {
                AdHocHost.Log("ActivationContext get failed. InvalidOp");
            }

            FabricRuntime runtime       = null;
            Action        exitedHandler = () =>
            {
                AdHocHost.Log("Fabric Exited");
                AdHocHost.hasFabricDied = true;

                // try creating the fabric runtime on a background thread
                // this should not av
                if (AdHocHost.recreateRuntimeOnCrash)
                {
                    Task.Factory.StartNew(() =>
                    {
                        while (true)
                        {
                            try
                            {
                                AdHocHost.Log("Creating again");
                                runtime = FabricRuntime.Create();
                                AdHocHost.Log("Recreated");
                                break;
                            }
                            catch (AccessViolationException)
                            {
                                AdHocHost.Log("AV");
                                throw;
                            }
                            catch (Exception ex)
                            {
                                AdHocHost.Log("Error {0} when re-creating", ex);
                            }
                        }
                    });
                }
            };

            AdHocHost.Log("Creating FabricRuntime");
            var runtimeTask = FabricRuntime.CreateAsync(exitedHandler, TimeSpan.FromSeconds(5), CancellationToken.None);

            runtimeTask.Wait();

            runtime = runtimeTask.Result;


            AdHocHost.Log("Registering factories");
            foreach (var item in AdHocHost.serviceTypes)
            {
                AdHocHost.Log("Registering: {0}", item.Name);

                var factory = new MyServiceFactory {
                    ServiceImplementationType = item
                };

                Task registerTask = null;
                if (factory.IsStateful)
                {
                    registerTask = runtime.RegisterStatefulServiceFactoryAsync(item.Name, factory, TimeSpan.FromSeconds(5), CancellationToken.None);
                }
                else
                {
                    registerTask = runtime.RegisterStatelessServiceFactoryAsync(item.Name, factory, TimeSpan.FromSeconds(5), CancellationToken.None);
                }

                registerTask.Wait();
            }

            AdHocHost.Log("Connecting to pipe");
            var pipeWrapper = new PipeWrapper(AdHocHost.pipeServerAddress);

            AdHocHost.Log("Waiting for quit...");
            shouldQuitNowEvent.WaitOne();

            return(0);
        }