Пример #1
0
        private static void ConfigureCommanding(CloudQueue queue, out ICommandDispatcher dispatcher, out IAzureStorageCommandQueueProcessorFactory listenerFactory)
        {
            ServiceCollection serviceCollection = new ServiceCollection();
            CommandingDependencyResolverAdapter dependencyResolver = serviceCollection.GetCommandingDependencyResolver(() => _serviceProvider);
            ICommandRegistry registry = dependencyResolver.AddCommanding();

            dependencyResolver.AddQueues(
                (msg, cmd, ex) =>
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(msg);
            },
                (msg, cmd, ex) =>
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(msg);
            },
                (msg, cmd, ex) =>
            {
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine(msg);
            })
            .AddAzureStorageCommanding();

            registry
            .Register <OutputWorldToConsoleCommandHandler>(1000, dispatcherFactoryFunc: queue.CreateCommandDispatcherFactory())
            .Register <OutputBigglesToConsoleCommandHandler>();

            _serviceProvider = serviceCollection.BuildServiceProvider();
            dispatcher       = _serviceProvider.GetService <ICommandDispatcher>();
            listenerFactory  = _serviceProvider.GetService <IAzureStorageCommandQueueProcessorFactory>();
        }
        private static IServiceBusCommandQueueProcessorFactory ConfigureForDequeue()
        {
            if (_dequeueServiceProvider == null)
            {
                IServiceCollection serviceCollection         = new ServiceCollection();
                CommandingDependencyResolverAdapter resolver = new CommandingDependencyResolverAdapter(
                    (fromType, toInstance) => serviceCollection.AddSingleton(fromType, toInstance),
                    (fromType, toType) => serviceCollection.AddTransient(fromType, toType),
                    (resolveType) => _dispatchServiceProvider.GetService(resolveType));
                ICommandRegistry commandRegistry = resolver.AddCommanding();
                resolver.AddQueues().AddAzureServiceBus();

                // register our command to dispatch to a servie bus queue
                commandRegistry.Register <SimpleCommandHandler>();

                _dequeueServiceProvider = serviceCollection.BuildServiceProvider();
            }

            IServiceBusCommandQueueProcessorFactory serviceBusCommandQueueProcessorFactory = _dispatchServiceProvider.GetService <IServiceBusCommandQueueProcessorFactory>();

            return(serviceBusCommandQueueProcessorFactory);
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public virtual void ConfigureServices(IServiceCollection services)
        {
            string storageAccountConnectionString = Configuration["storage:connectionstring"];
            string expensiveOperationQueueName    = Configuration["storage:queuename"];

            CommandingDependencyResolverAdapter resolver = new CommandingDependencyResolverAdapter(
                (fromType, toInstance) => services.AddSingleton(fromType, toInstance),
                (fromType, toType) => services.AddTransient(fromType, toType),
                (resolveTo) => _serviceProvider.GetService(resolveTo));

            // Using an instance of CommandingRuntime rather than the static helpers isolates
            // this commanding infrastructure to this startup class / instance of the web app
            // which means that acceptance tests can be run in parallel with multiple instances
            // of the ASP.Net Core test server.
            ICommandRegistry registry = _commandingRuntime.AddCommanding(resolver);

            //ICommandRegistry registry = resolver.AddCommanding();
            resolver.AddQueues().AddAzureStorageCommanding();

            // Register our command handlers using the discovery approach. Our handlers are in this assembly
            // so we just pass through our assembly
            registry.Discover(typeof(Startup).Assembly);

            // Register our expensive operation command to be sent to a queue
            registry.Register <ExpensiveOperationCommand>(
                CloudQueueDispatcherFactory.Create(storageAccountConnectionString, expensiveOperationQueueName));

            // Register our commands as REST endpoints. This results in an API where the AddCommand, GetPostsQuery,
            // GetPostsForCurrentUserQuery and GetPostQuery are handled as GET requests and executed immediately
            // in process by the registered handlers while the ExpensiveOperationCommand is exposed as a POST operation
            // and results in the command being placed on a queue
            services
            .AddMvc(ConfigureMvcOptions)
            // this block configures our commands to be exposed on endpoints
            .AddAspNetCoreCommanding(cfg => cfg
                                     .Controller("Post", controller => controller
                                                 .Action <GetPostsQuery>(HttpMethod.Get)
                                                 .Action <GetPostQuery, FromRouteAttribute>(HttpMethod.Get, "{PostId}")
                                                 .Action <AddNewPostCommand>(HttpMethod.Post)
                                                 .Action <DeletePostCommand>(HttpMethod.Delete, "{PostId}")
                                                 )
                                     .Controller("Profile", controller => controller
                                                 .Action <GetPostsForCurrentUserQuery>(HttpMethod.Get, "Posts"))
                                     .Controller("ExpensiveOperation", controller => controller
                                                 .Action <ExpensiveOperationCommand>(HttpMethod.Post))
                                     .Controller("SecurityTest", controller => controller
                                                 .Action <SecurityTestCommand>(HttpMethod.Post)
                                                 // we wire this one up with an ActionDefinition model to cover that test path
                                                 .Action(new ActionDefinition
            {
                BindingAttributeType = typeof(FromQueryAttribute),
                CommandType          = typeof(SecurityTestCommand),
                ResultType           = null,
                Route = "asQueryParam",
                Verb  = HttpMethod.Get
            }))
                                     .Claims(ConfigureClaimsMapping)
                                     .LogControllerCode(code =>
            {
                // this will output the code that is compiled for each controller to the debug window
                Debug.WriteLine(code);
            })
                                     );

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info {
                    Title = "Acceptance Test API", Version = "v1"
                });
                c.AddAspNetCoreCommanding();
            });
        }