public void Add_ignoreDuplicationをfalseにすると同じパスは例外が発生する() { var registry = new ServiceRegistry(); registry.Add(typeof(ServicePing)); Action act = () => registry.Add(typeof(ServicePing2), false); act.Should().Throw <ArgumentException>(); registry.GetEndpoints().Should().HaveCount(1); }
public void Add_ignoreDuplicationをtrueにすると同じパスは無視される() { var registry = new ServiceRegistry(); registry.Add(typeof(ServicePing)); registry.Add(typeof(ServicePing2), true); registry.GetEndpoints().Should().HaveCount(1); var type = registry.GetService("/ping").ServiceType; type.Should().Be(typeof(ServicePing)); }
public void Add_typeがnullの場合は例外が発生する() { var registry = new ServiceRegistry(); Action act = () => registry.Add(null); act.Should().Throw <ArgumentNullException>(); }
public async Task T_SendAsync_Filterが設定されている場合は順番に実行される() { var services = new ServiceCollection(); services.AddMediatR(typeof(ServicePing).Assembly); var check = new FilterExecutionCheck(); services.AddSingleton(check); var handlerCheck = new ExecuteCheck(); services.AddTransient(_ => handlerCheck); var provider = services.BuildServiceProvider(); var serviceFactory = provider.GetService<ServiceFactory>(); var registry = new ServiceRegistry(); registry.Add(typeof(ServicePing), typeof(Filter1), typeof(Filter2)); var client = new DefaultMediatorClient(registry, new ListenerRegistry(), serviceFactory!, new InternalScopedServiceFactoryFactory(serviceFactory!), new TestLogger()); var pong = await client.SendAsync<Pong>("/ping", new ServicePing() { Value = "Ping" }); pong!.Value.Should().Be("Ping Pong"); check.Checks.Should().HaveCount(2); check.Checks[0].Should().Be("1"); check.Checks[1].Should().Be("2"); }
public void Add_typeがnullの場合は例外が発生する_withIgnoreDuplication() { var registry = new ServiceRegistry(); Action act = () => registry.Add(null, true); act.Should().Throw <ArgumentNullException>(); }
public void ConfigureContainer(ServiceRegistry services) { services.Add(new ServiceDescriptor( typeof(IMessageMaker), sp => new MessageMaker("Bug159"), ServiceLifetime.Transient) ); }
/// <summary> /// Script load /// </summary> public LegacyCoreServer() { // Setting singleton instance and subscribing to ticking Instance = this; Tick += OnTick; // Setup database and create it Db = new DB(); Db.Database.CreateIfNotExists(); // Load players LegacyPlayers.Instance.Load(); // Register services Services.Add(new CommandService()); Services.Add(new ConnectService()); Services.Initialize(); }
public void Add_AsServiceが付与されている場合は追加される() { var registry = new ServiceRegistry(); registry.Add(typeof(ServicePing)); var req = registry.GetService("/ping"); req.Should().NotBeNull(); registry.GetEndpoints().Should().HaveCount(1); }
public void Add_Filterを設定しないとMediatorRequestに設定されない() { var registry = new ServiceRegistry(); registry.Add(typeof(ServicePing)); var request = registry.GetService("/ping"); request.Filters.Should().HaveCount(0); }
public void Resolve_GivenFuncRegisteredService_ResolvesAsExpected(ServiceLifetime lifetime) { // Arrange IServiceCollection serviceCollection = new ServiceRegistry(); serviceCollection.Add(new ServiceDescriptor(typeof(Service), typeof(Service), lifetime)); serviceCollection.Add(new ServiceDescriptor(typeof(ServiceWithFunc), typeof(ServiceWithFunc), lifetime)); IServiceProvider serviceProvider = new Container(serviceCollection); using (IServiceScope scope = serviceProvider.CreateScope()) { // Act Action action = () => scope.ServiceProvider.GetRequiredService <ServiceWithFunc>(); // Assert action .Should() .NotThrow(); } }
public void ScanTypes(TypeSet types, ServiceRegistry services) { foreach (var type in types.FindTypes(TypeClassification.Concretes) .Where(type => type.GetConstructors().Any())) { var serviceType = FindPluginType(type); if (serviceType != null && ShouldAdd(services, serviceType, type)) { services.Add(new ServiceDescriptor(serviceType, type, _lifetime)); } } }
public static IServiceCollection AddMediatRServiceLike(this IServiceCollection services, Assembly assembly, params Type[] filterTypes) { if (!(services.LastOrDefault(s => s.ServiceType == typeof(ServiceRegistry))? .ImplementationInstance is ServiceRegistry serviceRegistry)) { serviceRegistry = new ServiceRegistry(); } services.TryAddSingleton(serviceRegistry); var serviceDescriptions = assembly.GetTypes() .Where(MediatorServiceDescription.CanServicalize); foreach (var serviceDescription in serviceDescriptions) { serviceRegistry.Add(serviceDescription, true, filterTypes); } if (!(services.LastOrDefault(s => s.ServiceType == typeof(ListenerRegistry))? .ImplementationInstance is ListenerRegistry listenerRegistry)) { listenerRegistry = new ListenerRegistry(); } services.TryAddSingleton(listenerRegistry); var listenerDescriptions = assembly.GetTypes() .Where(MediatorListenerDescription.CanListenerize); foreach (var listenerDescription in listenerDescriptions) { listenerRegistry.Add(listenerDescription); } services.TryAddSingleton <IServiceLikeContextAccessor, ServiceLikeContextAccessor>(); services.TryAddTransient(typeof(IMediatorClient), provider => { var servRegistry = provider.GetService <ServiceRegistry>(); var lisRegistry = provider.GetService <ListenerRegistry>(); var serviceFactory = provider.GetService <ServiceFactory>(); var scopedServiceFactoryFactory = new ScopedServiceFactoryFactory(provider.GetService <IServiceScopeFactory>()); var serviceLikeLogger = provider.GetService <IServiceLikeLogger>(); return(new DefaultMediatorClient( servRegistry, lisRegistry, serviceFactory, scopedServiceFactoryFactory, serviceLikeLogger)); }); return(services); }
public void Add_設定したFilterがMediatorRequestに設定されている() { var registry = new ServiceRegistry(); registry.Add(typeof(ServicePing), typeof(Filter1), typeof(Filter2)); var request = registry.GetService("/ping"); request.Filters.Should().HaveCount(2); var filters = request.Filters.ToList(); filters[0].Should().Be(typeof(Filter1)); filters[1].Should().Be(typeof(Filter2)); }
public void Add_AsServiceが複数付与されている場合は複数追加される() { var registry = new ServiceRegistry(); registry.Add(typeof(MultiServicePing)); var req = registry.GetService("/ping/1"); req.Should().NotBeNull(); var req2 = registry.GetService("/ping/2"); req2.Should().NotBeNull(); registry.GetEndpoints().Should().HaveCount(2); }
public void ScanTypes(TypeSet types, ServiceRegistry services) { var interfaces = types.FindTypes(TypeClassification.Interfaces | TypeClassification.Closed) .Where(x => x != typeof(IDisposable)); var concretes = types.FindTypes(TypeClassification.Concretes | TypeClassification.Closed) .Where(x => x.GetConstructors().Any()).ToArray(); interfaces.Each(@interface => { var implementors = concretes.Where(x => x.CanBeCastTo(@interface)).ToArray(); if (implementors.Count() == 1) { services.Add(new ServiceDescriptor(@interface, implementors.Single(), _lifetime)); } }); }
public void Add_RequestにFilterが設定されている場合は末尾に追加される() { var registry = new ServiceRegistry(); registry.Add(typeof(ServiceWithFilterPing), typeof(Filter2), typeof(Filter3)); var request = registry.GetService("/ping"); request.Filters.Should().HaveCount(4); var filters = request.Filters.ToList(); filters[0].Should().Be(typeof(Filter2)); filters[1].Should().Be(typeof(Filter3)); filters[2].Should().Be(typeof(Filter1)); filters[3].Should().Be(typeof(Filter4)); }
public async Task SendAsync_Mediatorが実行されるが結果は捨てられる() { var services = new ServiceCollection(); services.AddMediatR(typeof(ServicePing).Assembly); var check = new ExecuteCheck(); services.AddTransient(_ => check); var provider = services.BuildServiceProvider(); var serviceFactory = provider.GetService<ServiceFactory>()!; var registry = new ServiceRegistry(); registry.Add(typeof(ServicePing)); var client = new DefaultMediatorClient(registry, new ListenerRegistry(), serviceFactory!, new InternalScopedServiceFactoryFactory(serviceFactory!), new TestLogger()); await client.SendAsync("/ping", new ServicePing() { Value = "Ping" }); check.Executed.Should().BeTrue(); }
public void ScanTypes( TypeSet types, ServiceRegistry services) { foreach (var type in types.AllTypes()) { var interfaceTypes = type.FindInterfacesThatClose(_openType).ToArray(); if (!interfaceTypes.Any()) { continue; } if (type.IsConcrete()) { _concretions.Add(type); } foreach (var interfaceType in interfaceTypes) { _interfaces.Fill(interfaceType); } } foreach (var @interface in _interfaces) { var exactMatches = _concretions.Where(x => x.CanBeCastTo(@interface)).ToArray(); foreach (var type in exactMatches) { services.Add(new ServiceDescriptor(@interface, type, _lifetime)); } if ([email protected]()) { addConcretionsThatCouldBeClosed(@interface, services); } } var concretions = services.ConnectedConcretions(); foreach (var type in _concretions) { concretions.Fill(type); } }
public async Task T_SendAsync_戻り値がUnitの場合はnullで返される() { var services = new ServiceCollection(); services.AddMediatR(typeof(ServicePing).Assembly); var check = new ExecuteCheck(); services.AddTransient(_ => check); var provider = services.BuildServiceProvider(); var serviceFactory = provider.GetService<ServiceFactory>(); var registry = new ServiceRegistry(); registry.Add(typeof(VoidServicePing)); var client = new DefaultMediatorClient(registry, new ListenerRegistry(), serviceFactory!, new InternalScopedServiceFactoryFactory(serviceFactory!), new TestLogger()); var pong = await client.SendAsync<Pong>("/ping/void", new { Value = "Ping" }); pong.Should().BeNull(); check.Executed.Should().BeTrue(); }
public async Task T_SendAsync_引数と戻り値は変換可能_Jsonシリアライズデシリアライズに依存() { var services = new ServiceCollection(); services.AddMediatR(typeof(ServicePing).Assembly); var check = new ExecuteCheck(); services.AddTransient(_ => check); var provider = services.BuildServiceProvider(); var serviceFactory = provider.GetService<ServiceFactory>(); var registry = new ServiceRegistry(); registry.Add(typeof(ServicePing)); var client = new DefaultMediatorClient(registry, new ListenerRegistry(), serviceFactory!, new InternalScopedServiceFactoryFactory(serviceFactory!), new TestLogger()); var pong = await client.SendAsync<LocalPong>("/ping", new { Value = "Ping" }); pong!.Value.Should().Be("Ping Pong"); check.Executed.Should().BeTrue(); }
public void Add_When_Null_Should_Throw_ArgumentNullException() { Assert.Throws <ArgumentNullException>(() => _underTest.Add(null)); }