/// <summary>
        /// Builds and returns a <see cref="TaskHubWorker"/> using the configurations from this instance.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <returns>A new <see cref="TaskHubWorker"/>.</returns>
        public TaskHubWorker Build(IServiceProvider serviceProvider)
        {
            Check.NotNull(serviceProvider, nameof(serviceProvider));

            if (OrchestrationService == null)
            {
                throw new InvalidOperationException(Strings.OrchestrationInstanceNull);
            }

            var worker = new TaskHubWorker(
                OrchestrationService,
                new WrapperObjectManager <TaskOrchestration>(_orchestrations, type => new WrapperOrchestration(type)),
                new WrapperObjectManager <TaskActivity>(_activities, type => new WrapperActivity(type)));

            // The first middleware added begins the service scope for all further middleware, the orchestration, and activities.
            worker.AddOrchestrationDispatcherMiddleware(BeginMiddlewareScope(serviceProvider));
            worker.AddOrchestrationDispatcherMiddleware(WrapMiddleware(typeof(ServiceProviderOrchestrationMiddleware)));
            foreach (Type middlewareType in _orchestrationsMiddleware)
            {
                worker.AddOrchestrationDispatcherMiddleware(WrapMiddleware(middlewareType));
            }

            worker.AddActivityDispatcherMiddleware(BeginMiddlewareScope(serviceProvider));
            worker.AddActivityDispatcherMiddleware(WrapMiddleware(typeof(ServiceProviderActivityMiddleware)));
            foreach (Type middlewareType in _activitiesMiddleware)
            {
                worker.AddActivityDispatcherMiddleware(WrapMiddleware(middlewareType));
            }

            return(worker);
        }
        /// <summary>
        /// Builds and returns a <see cref="TaskHubWorker"/> using the configurations from this instance.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <returns>A new <see cref="TaskHubWorker"/>.</returns>
        public TaskHubWorker Build(IServiceProvider serviceProvider)
        {
            Check.NotNull(serviceProvider, nameof(serviceProvider));

            if (OrchestrationService is null)
            {
                OrchestrationService = serviceProvider.GetRequiredService <IOrchestrationService>();
            }

            // Verify we still have our ServiceProvider middleware
            if (OrchestrationMiddleware.FirstOrDefault(x => x.Type == typeof(ServiceProviderOrchestrationMiddleware))
                is null)
            {
                throw new InvalidOperationException(Strings.ExpectedMiddlewareMissing(
                                                        typeof(ServiceProviderOrchestrationMiddleware), nameof(OrchestrationMiddleware)));
            }

            if (ActivityMiddleware.FirstOrDefault(x => x.Type == typeof(ServiceProviderActivityMiddleware)) is null)
            {
                throw new InvalidOperationException(Strings.ExpectedMiddlewareMissing(
                                                        typeof(ServiceProviderActivityMiddleware), nameof(ActivityMiddleware)));
            }

            ILoggerFactory loggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>();
            var            worker        = new TaskHubWorker(
                OrchestrationService,
                new GenericObjectManager <TaskOrchestration>(),
                new GenericObjectManager <TaskActivity>(),
                loggerFactory);

            worker.AddTaskOrchestrations(Orchestrations.Select(x => new OrchestrationObjectCreator(x)).ToArray());
            worker.AddTaskActivities(Activities.Select(x => new ActivityObjectCreator(x)).ToArray());

            // The first middleware added begins the service scope for all further middleware, the orchestration, and
            // activities.
            worker.AddOrchestrationDispatcherMiddleware(BeginMiddlewareScope(serviceProvider));
            foreach (TaskMiddlewareDescriptor middlewareDescriptor in OrchestrationMiddleware)
            {
                worker.AddOrchestrationDispatcherMiddleware(WrapMiddleware(middlewareDescriptor));
            }

            worker.AddActivityDispatcherMiddleware(BeginMiddlewareScope(serviceProvider));
            foreach (TaskMiddlewareDescriptor middlewareDescriptor in ActivityMiddleware)
            {
                worker.AddActivityDispatcherMiddleware(WrapMiddleware(middlewareDescriptor));
            }

            return(worker);
        }
        internal TaskHubWorker Build(IServiceProvider provider)
        {
            var orchestrationService         = provider.GetRequiredService <IOrchestrationService>();
            var extendedOrchestrationService = provider.GetService <IExtendedOrchestrationService>();
            var serviceScopeFactory          = provider.GetRequiredService <IServiceScopeFactory>();

            var orchestrations = provider.GetServices <ObjectCreator <TaskOrchestration> >().ToArray();
            var activities     = provider.GetServices <ObjectCreator <TaskActivity> >().ToArray();

            var serviceProviderOrchestrationService = new WorkerOrchestrationService(
                orchestrationService,
                extendedOrchestrationService,
                serviceScopeFactory,
                orchestrations,
                activities,
                HasAllOrchestrations,
                HasAllActivities);

            var loggerFactory = provider.GetService <ILoggerFactory>();

            var taskHubWorker = new TaskHubWorker(serviceProviderOrchestrationService, loggerFactory);

            // Orchestration middlewares
            var serviceProviderOrchestrationMiddleware = provider.GetRequiredService <ServiceProviderOrchestrationMiddleware>();

            taskHubWorker.AddOrchestrationDispatcherMiddleware(serviceProviderOrchestrationMiddleware.Execute);
            foreach (var middleware in _orchestrationMiddlewares)
            {
                taskHubWorker.AddOrchestrationDispatcherMiddleware(CreateMiddleware(middleware));
            }

            // Activitie middlewares
            var serviceProviderActivityMiddleware = provider.GetRequiredService <ServiceProviderActivityMiddleware>();

            taskHubWorker.AddActivityDispatcherMiddleware(serviceProviderActivityMiddleware.Execute);
            foreach (var factory in _activitiesMiddlewares)
            {
                taskHubWorker.AddActivityDispatcherMiddleware(CreateMiddleware(factory));
            }

            // Orchestrations and activities
            taskHubWorker.AddTaskOrchestrations(orchestrations);
            taskHubWorker.AddTaskActivities(activities);

            return(taskHubWorker);
        }
Ejemplo n.º 4
0
        static async Task Main(string[] args)
        {
            //Use Azurite emulator
            string storageConnectionString = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;" +
                                             "BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1";
            string taskHubName = "Hello";

            var settings = new AzureStorageOrchestrationServiceSettings();

            settings.StorageConnectionString      = storageConnectionString;
            settings.TaskHubName                  = taskHubName;
            settings.UseDataContractSerialization = true;

            var orchestrationServiceAndClient = new AzureStorageOrchestrationService(settings);
            await orchestrationServiceAndClient.CreateIfNotExistsAsync();

            var taskHubClient = new TaskHubClient(orchestrationServiceAndClient);
            var taskHub       = new TaskHubWorker(orchestrationServiceAndClient);

            // add instance
            _ = Task.Run(async() =>
            {
                OrchestrationInstance ins = await taskHubClient.CreateOrchestrationInstanceAsync(typeof(HelloOrchestration), "");
            });

            // add worker
            try
            {
                taskHub.AddTaskOrchestrations(
                    typeof(HelloOrchestration)
                    );

                taskHub.AddTaskActivities(typeof(HelloTask));
                taskHub.AddOrchestrationDispatcherMiddleware(async(context, next) =>
                {
                    OrchestrationRuntimeState runtimeState = context.GetProperty <OrchestrationRuntimeState>();
                    var customInstance      = OrchestrationInstanceEx.Initialize(runtimeState);
                    customInstance.Dic["a"] = "a";
                    customInstance.Dic["b"] = "b";
                    customInstance.Dic["c"] = "c";
                    context.SetProperty <OrchestrationInstance>(customInstance);

                    await next();
                });

                taskHub.AddOrchestrationDispatcherMiddleware(async(context, next) =>
                {
                    var customInstance = OrchestrationInstanceEx.Get(context.GetProperty <OrchestrationInstance>());
                    context.SetProperty <OrchestrationInstance>(customInstance);

                    //Dic data can get here. But missed in HelloOrchestration context and ActivityDispatcherMiddleware

                    await next();
                });

                taskHub.AddActivityDispatcherMiddleware(async(context, next) =>
                {
                    var customInstance = OrchestrationInstanceEx.Get(context.GetProperty <OrchestrationInstance>());
                    context.SetProperty <OrchestrationInstance>(customInstance);

                    //Dic data missed

                    await next();
                });

                await taskHub.StartAsync();

                //Console.WriteLine("Press any key to quit.");
                Console.ReadLine();
                taskHub.StopAsync(true).Wait();
            }
            catch (Exception e)
            {
                // silently eat any unhandled exceptions.
                Console.WriteLine($"worker exception: {e}");
            }
        }