public static IServiceCollection AddNotificationHandlers(this IServiceCollection services, params Type[] markerTypes)
        {
            var assemblies = markerTypes.Select(x => x.GetTypeInfo().Assembly);

            ServiceRegistrar.AddMediatRClasses(services, assemblies);
            return(services);
        }
        public void RegisterAssembly(Assembly assembly)
        {
            IList <Assembly> assemblies = new List <Assembly>()
            {
                assembly
            };

            ServiceRegistrar.AddMediatRClasses(HostManager.Context.ServiceDescriptors, assemblies);
        }
        public static void AddStatistics(this IServiceCollection serviceCollection, string connectionString)
        {
            ServiceRegistrar.AddMediatRClasses(serviceCollection, new[] { Assembly });

            serviceCollection.AddDbContext <StatisticsDbContext>(options =>
                                                                 options.UseSqlServer(connectionString,
                                                                                      x => x.MigrationsHistoryTable("__EFMigrationsHistory", "Stats")),
                                                                 ServiceLifetime.Transient);
        }
Exemple #4
0
        public MediatorHandlerTests()
        {
            services = new ServiceContainer();

            ServiceRegistrar.AddMediatRClasses(services, new[] { Assembly.GetExecutingAssembly() });
            ServiceRegistrar.AddRequiredServices(services);

            mediator = services.GetInstance <IMediator>();
        }
        public static ElsaOptionsBuilder UseIndexing(this ElsaOptionsBuilder options, Action <ElsaIndexingOptions> configure)
        {
            var indexingOptions = new ElsaIndexingOptions(options.Services);

            configure.Invoke(indexingOptions);

            ServiceRegistrar.AddMediatRClasses(options.Services, new[] { Assembly.GetExecutingAssembly() });

            return(options);
        }
        public void AddRequiredServices_WhenCalled_ShouldContainExpectedInstances()
        {
            ServiceRegistrar.AddMediatRClasses(services, assemblies);
            ServiceRegistrar.AddRequiredServices(services);

            services.GetInstance <IMediator>().ShouldBeOfType <Mediator>();
            services.GetInstance <ISender>().ShouldBeOfType <Mediator>();
            services.GetInstance <IPublisher>().ShouldBeOfType <Mediator>();
            services.AvailableServices.Where(x => x.ServiceType == typeof(INotificationHandler <PingNotification>)).Count().ShouldBe(3);
        }
        protected override TestServer CreateServer(IWebHostBuilder builder)
        {
            builder.ConfigureTestServices(services =>
            {
                ServiceRegistrar.AddMediatRClasses(services, new[] { typeof(TestWebApplicationFactory).Assembly });

                var serviceDescriptor = services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IGetPagedFileListQueryFactory));
                services.Remove(serviceDescriptor);

                services.AddTransient <IGetPagedFileListQueryFactory, GetPagedFileListQueryFactoryMock>();
            });
            return(base.CreateServer(builder));
        }
Exemple #8
0
 public static IServiceCollection AddJsonRpcMediatR(this IServiceCollection services, IEnumerable <Assembly> assemblies)
 {
     ServiceRegistrar.AddRequiredServices(services, new MediatRServiceConfiguration());
     ServiceRegistrar.AddMediatRClasses(services, assemblies);
     services.AddScoped <IRequestContext, RequestContext>();
     services.RemoveAll <ServiceFactory>();
     services.AddScoped <ServiceFactory>(
         serviceProvider => {
         return(serviceType => GetHandler(serviceProvider, serviceType));
     }
         );
     return(services);
 }
        /// <summary>
        /// Registers the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="configuration">The configuration.</param>
        /// <param name="services">The services.</param>
        public void Register(IConventionContext context, IConfiguration configuration, IServiceCollection services)
        {
            var serviceConfig = context.GetOrAdd(() => new MediatRServiceConfiguration());

            if (services.Any(z => z.ServiceType == typeof(IMediator)))
            {
                return;
            }

            context.Set(serviceConfig);
            var assemblies = context.AssemblyCandidateFinder
                             .GetCandidateAssemblies(nameof(MediatR)).ToArray();

            ServiceRegistrar.AddRequiredServices(services, serviceConfig);
            ServiceRegistrar.AddMediatRClasses(services, assemblies);
        }
Exemple #10
0
        /// <summary>
        /// Registers handlers and mediator types from the specified assemblies
        /// </summary>
        /// <param name="services">Service collection</param>
        /// <param name="assemblies">Assemblies to scan</param>
        /// <param name="configuration">The action used to configure the options</param>
        /// <returns>Service collection</returns>
        public static IServiceCollection AddMediatR(this IServiceCollection services, IEnumerable <Assembly> assemblies, Action <MediatRServiceConfiguration> configuration)
        {
            if (!assemblies.Any())
            {
                throw new ArgumentException("No assemblies found to scan. Supply at least one assembly to scan for handlers.");
            }
            var serviceConfig = new MediatRServiceConfiguration();

            configuration?.Invoke(serviceConfig);

            ServiceRegistrar.AddRequiredServices(services, serviceConfig);

            ServiceRegistrar.AddMediatRClasses(services, assemblies);

            return(services);
        }
        /// <summary>
        /// Adds PineBlog core services to the specified services collection.
        /// </summary>
        /// <param name="services">The services available in the application.</param>
        /// <param name="configuration">The application configuration properties.</param>
        /// <remarks>This only adds the PineBlog core services not that data layer.
        /// To add the data layer use one of the specific methods for that, e.g. AddPineBlogEntityFrameworkCore.</remarks>
        /// <returns>The original services object.</returns>
        public static IServiceCollection AddPineBlogCore(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddOptions();
            services.Configure <PineBlogOptions>(configuration.GetSection(nameof(PineBlogOptions)));

            if (services.BuildServiceProvider().GetService <IMediator>() == null)
            {
                services.AddMediatR(typeof(AddPostCommand).Assembly);
            }
            ServiceRegistrar.AddMediatRClasses(services, new[] { typeof(AddPostCommand).Assembly });

            services.AddTransient(typeof(IPipelineBehavior <,>), typeof(RequestValidationBehavior <,>));

            services.AddTransient <IValidator <AddPostCommand>, AddPostCommandValidator>();
            services.AddTransient <IValidator <UpdatePostCommand>, UpdatePostCommandValidator>();
            services.AddTransient <IValidator <PublishPostCommand>, PublishPostCommandValidator>();
            services.AddTransient <IValidator <UnpublishPostCommand>, UnpublishPostCommandValidator>();
            services.AddTransient <IValidator <DeletePostCommand>, DeletePostCommandValidator>();

            services.AddTransient <IValidator <GetPostQuery>, GetPostQueryValidator>();
            services.AddTransient <IValidator <GetPostByIdQuery>, GetPostByIdQueryValidator>();

            services.AddTransient <IValidator <GetSyndicationFeedQuery>, GetSyndicationFeedQueryValidator>();

            services.AddTransient <IValidator <UpdateBlogSettingsCommand>, UpdateBlogSettingsCommandValidator>();

            services.AddTransient <IPostRanker, PostRanker>();

            services.AddTransient <IUploadFileCommandFactory, UploadFileCommandFactory>();
            services.AddTransient <IDeleteFileCommandFactory, DeleteFileCommandFactory>();
            services.AddTransient <IGetPagedFileListQueryFactory, GetPagedFileListQueryFactory>();

            services.AddTransient <FileUrlHelper>();
            services.AddTransient <PostUrlHelper>();
            services.AddTransient <AzureBlobHelper>();

            services.AddPineBlogCoreAzureServices();
            services.AddFeatureManagement();

            return(services);
        }
Exemple #12
0
 public static IServiceCollection AddDomain(this IServiceCollection services)
 {
     ServiceRegistrar.AddMediatRClasses(services, new[] { Assembly.GetExecutingAssembly() });
     return(services);
 }
Exemple #13
0
        public IAddRemotiatrOptions AddHost(Uri rootUri, Func <Type, Uri> pathLocator, Type messageSerializerType, Type messageTransportType, params Assembly[] assemblies)
        {
            if (rootUri == null)
            {
                throw new ArgumentNullException(nameof(rootUri));
            }
            if (!rootUri.IsAbsoluteUri)
            {
                throw new ArgumentException($"{nameof(rootUri)} must be absolute");
            }
            if (rootUri != new Uri($"{rootUri.Scheme}://{rootUri.Authority}"))
            {
                throw new ArgumentException($"{nameof(rootUri)} must only contain URI scheme and authority");
            }
            if (pathLocator == null)
            {
                throw new ArgumentNullException(nameof(pathLocator));
            }
            if (messageSerializerType == null)
            {
                throw new ArgumentNullException(nameof(messageSerializerType));
            }
            if (!typeof(IMessageSerializer).IsAssignableFrom(messageSerializerType))
            {
                throw new ArgumentException($"{nameof(messageSerializerType)} must be assignable to {typeof(IMessageSerializer).FullName}");
            }
            if (messageTransportType == null)
            {
                throw new ArgumentNullException(nameof(messageTransportType));
            }
            if (!typeof(IMessageTransport).IsAssignableFrom(messageTransportType))
            {
                throw new ArgumentException($"{nameof(messageTransportType)} must be assignable to {typeof(IMessageTransport).FullName}");
            }
            if (assemblies == null)
            {
                throw new ArgumentNullException(nameof(assemblies));
            }

            assemblies = assemblies.Distinct().ToArray();

            foreach (var assembly in assemblies)
            {
                if (!_registeredAssemblies.Add(assembly))
                {
                    throw new InvalidOperationException($"Assembly {assembly.FullName} was previously registered. Assemblies must only be registered once");
                }
            }

            var notificationTypes = assemblies
                                    .SelectMany(x => x.GetTypes())
                                    .Where(x => x.IsNotificationType())
                                    .ToArray();

            var requestTypes = assemblies
                               .SelectMany(x => x.GetTypes())
                               .Where(x => x.IsRequestType())
                               .ToArray();

            var messageInfos = new List <MessageInfo>();

            foreach (var messageType in notificationTypes.Concat(requestTypes))
            {
                var pathUri = pathLocator(messageType);
                if (pathUri.IsAbsoluteUri)
                {
                    throw new InvalidOperationException($"{nameof(pathLocator)} must only provide relative URIs");
                }

                var messageInfo = new MessageInfo(messageType, pathUri);

                AddHandler(messageInfo);

                messageInfos.Add(messageInfo);
            }

            var messageHostInfo = new MessageHostInfo(rootUri, messageSerializerType, messageTransportType, messageInfos);

            foreach (var messageInfo in messageInfos)
            {
                messageInfo.SetHost(messageHostInfo);
            }

            Services.AddSingleton <IMessageHostInfo>(messageHostInfo);

            ServiceRegistrar.AddMediatRClasses(Services, assemblies);

            return(this);
        }
 public static void AddDataSync(this IServiceCollection serviceCollection)
 {
     ServiceRegistrar.AddMediatRClasses(serviceCollection, new[] { Assembly });
 }
        public void AddMediatRClasses_WhenCalled_ShouldIncludeAllHandlers()
        {
            ServiceRegistrar.AddMediatRClasses(services, assemblies);

            services.AvailableServices.Count().ShouldBe(5);
        }
 public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
 {
     ServiceRegistrar.AddMediatRClasses(services, new[] { Assembly.GetExecutingAssembly() });
     return(services);
 }
Exemple #17
0
        public static IServiceCollection AddMediatR(this IServiceCollection services)
        {
            ServiceRegistrar.AddMediatRClasses(services, handlerAssemblyMarkerTypes.ToArray());

            return(services);
        }