public async Task Should_use_it_for_component_resolution() { var container = new AcceptanceTestingContainer(); container.RegisterSingleton(typeof(MyComponent), myComponent); var result = await Scenario.Define <Context>() .WithEndpoint <ExternallyManagedContainerEndpoint>(b => { IStartableEndpointWithExternallyManagedContainer configuredEndpoint = null; b.ToCreateInstance( config => { configuredEndpoint = EndpointWithExternallyManagedContainer.Create(config, new RegistrationPhaseAdapter(container)); return(Task.FromResult(configuredEndpoint)); }, configured => configured.Start(new ResolutionPhaseAdapter(container)) ) .When((e, c) => { c.BuilderWasResolvable = container.Build(typeof(IBuilder)) != null; //use the session provided by configure to make sure its properly populated return(configuredEndpoint.MessageSession.Value.SendLocal(new SomeMessage())); }); }) .Done(c => c.Message != null) .Run(); Assert.AreEqual(result.Message, myComponent.Message); Assert.True(result.BuilderWasResolvable, "IBuilder should be resolvable in the container"); Assert.False(container.WasDisposed, "Externally managed containers should not be disposed"); }
public async Task RegisterComponents_calls_override_registrations() { var serviceCollection = new ServiceCollection(); var context = await Scenario.Define <Context>() .WithEndpoint <EndpointWithOverrides>(b => b .ToCreateInstance( config => { serviceCollection.AddSingleton <IDependencyBeforeEndpointConfiguration, OriginallyDefinedDependency>(); var configuredEndpoint = EndpointWithExternallyManagedContainer.Create(config, serviceCollection); return(Task.FromResult(configuredEndpoint)); }, configured => { serviceCollection.AddSingleton <IDependencyBeforeEndpointStart, OriginallyDefinedDependency>(); return(configured.Start(serviceCollection.BuildServiceProvider())); })) .Done(c => c.EndpointsStarted) .Run(); Assert.IsInstanceOf <OverridenDependency>(context.DependencyFromFeature); Assert.IsInstanceOf <OverridenDependency>(context.DependencyBeforeEndpointConfiguration); // Registrations after the startable endpoint has been created can't be overriden by the RegisterComponents API Assert.IsInstanceOf <OriginallyDefinedDependency>(context.DependencyBeforeEndpointStart); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure <CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddSignalR(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); var endpointConfig = new EndpointConfiguration("TestObservableSaga"); var persistence = endpointConfig.UsePersistence <InMemoryPersistence>(); //var settings = persistence.SagaSettings(); endpointConfig.UseSerialization <NewtonsoftSerializer>(); var transport = endpointConfig.UseTransport <LearningTransport>(); var routing = transport.Routing(); routing.RouteToEndpoint( assembly: typeof(Startup).Assembly, destination: "ObservableSaga.Endpoint"); var startableEndpoint = EndpointWithExternallyManagedContainer.Create(endpointConfig, new ServiceCollectionAdapter(services)); services.AddSingleton(_ => startableEndpoint.MessageSession.Value); services.AddSingleton <IHostedService>(serviceProvider => new NServiceBusService(startableEndpoint, serviceProvider)); }
public void Should_not_resolve_until_start() { var endpointConfiguration = new EndpointConfiguration("custom-docstore-endpoint"); var typesToInclude = new List <Type> { typeof(MySaga) }; //need to include the NServiceBus.RavenDB types since the features need to be discovered typesToInclude.AddRange(typeof(RavenDBPersistence).Assembly.GetTypes()); endpointConfiguration.TypesToIncludeInScan(typesToInclude); endpointConfiguration.UseTransport <AcceptanceTestingTransport>(); endpointConfiguration.EnableOutbox(); endpointConfiguration.UsePersistence <RavenDBPersistence>() .SetDefaultDocumentStore(_ => { Assert.Fail("Document store resolved to early"); return(null); }); EndpointWithExternallyManagedContainer.Create(endpointConfiguration, new FakeContainerRegistration()); }
public async Task Should_use_it_for_component_resolution() { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton(typeof(MyComponent), myComponent); var result = await Scenario.Define <Context>() .WithEndpoint <ExternallyManagedContainerEndpoint>(b => { IStartableEndpointWithExternallyManagedContainer configuredEndpoint = null; b.ToCreateInstance( config => { configuredEndpoint = EndpointWithExternallyManagedContainer.Create(config, serviceCollection); return(Task.FromResult(configuredEndpoint)); }, configured => configured.Start(serviceCollection.BuildServiceProvider()) ) .When((e, c) => { //use the session provided by configure to make sure its properly populated return(configuredEndpoint.MessageSession.Value.SendLocal(new SomeMessage())); }); }) .Done(c => c.MessageReceived) .Run(); Assert.NotNull(result.ServiceProvider, "IServiceProvider should be injectable"); Assert.AreSame(myComponent, result.CustomService, "Should inject custom services"); }
public static IServiceCollection AddNServiceBus(this IServiceCollection services, EndpointConfiguration configuration) { var startableEndpoint = EndpointWithExternallyManagedContainer.Create(configuration, new ServiceCollectionAdapter(services)); services.AddSingleton(_ => startableEndpoint.MessageSession.Value); services.AddSingleton <IHostedService>(serviceProvider => new NServiceBusService(startableEndpoint, serviceProvider)); return(services); }
public void Should_throw() { Assert.Throws <InvalidOperationException>(() => { var container = new AcceptanceTestingContainer(); var endpointConfiguration = new EndpointConfiguration("MyEndpoint"); endpointConfiguration.UseContainer(container); EndpointWithExternallyManagedContainer.Create(endpointConfiguration, new RegistrationPhaseAdapter(container)); }); }
public void Should_not_resolve_until_start() { var endpointConfiguration = new EndpointConfiguration("custom-docstore-endpoint"); endpointConfiguration.AssemblyScanner().ExcludeAssemblies("NServiceBus.RavenDB.Tests"); endpointConfiguration.UseTransport(new LearningTransport()); endpointConfiguration.EnableOutbox(); endpointConfiguration.UsePersistence <RavenDBPersistence>() .SetDefaultDocumentStore((_, __) => { Assert.Fail("Document store resolved to early"); return(null); }); EndpointWithExternallyManagedContainer.Create(endpointConfiguration, new ServiceCollection()); }
async Task Usage(EndpointConfiguration endpointConfiguration) { #region ExternalPrepare IServiceCollection serviceCollection = new ServiceCollection(); var startableEndpoint = EndpointWithExternallyManagedContainer.Create(endpointConfiguration, serviceCollection); #endregion #region ExternalStart IServiceProvider builder = serviceCollection.BuildServiceProvider(); var startedEndpoint = await startableEndpoint.Start(builder); #endregion }
async Task Usage(EndpointConfiguration endpointConfiguration, MyCustomContainer myCustomContainer) { #region ExternalPrepare IConfigureComponents configureComponents = AdaptContainerForRegistrationPhase(myCustomContainer); var startableEndpoint = EndpointWithExternallyManagedContainer.Create(endpointConfiguration, configureComponents); #endregion #region ExternalStart IBuilder builder = AdaptContainerForResolutionPhase(myCustomContainer); var startedEndpoint = await startableEndpoint.Start(builder); #endregion }
static async Task Main() { Console.Title = "Samples.NServiceBus.ExternallyManagedContainer"; var endpointConfiguration = new EndpointConfiguration("Sample"); endpointConfiguration.UseTransport <LearningTransport>(); #region ContainerConfiguration // ServiceCollection is provided by Microsoft.Extensions.DependencyInjection var serviceCollection = new ServiceCollection(); // most dependencies may now be registered serviceCollection.AddSingleton <Greeter>(); serviceCollection.AddSingleton <MessageSender>(); // EndpointWithExternallyManagedContainer.Create accepts an IServiceCollection, // which is inherited by ServiceCollection var endpointWithExternallyManagedContainer = EndpointWithExternallyManagedContainer .Create(endpointConfiguration, serviceCollection); // if IMessageSession is required as dependency, it may now be registered serviceCollection.AddSingleton(p => endpointWithExternallyManagedContainer.MessageSession.Value); #endregion using (var serviceProvider = serviceCollection.BuildServiceProvider()) { var endpoint = await endpointWithExternallyManagedContainer.Start(serviceProvider) .ConfigureAwait(false); var sender = serviceProvider.GetRequiredService <MessageSender>(); await sender.SendMessage() .ConfigureAwait(false); Console.WriteLine("Press any key to exit"); Console.ReadKey(); await endpoint.Stop() .ConfigureAwait(false); } }
public async Task Make_sure_things_are_in_DI() { var serviceCollection = new ServiceCollection(); SpyConainer spyContainer = null; await Scenario.Define <Context>() .WithEndpoint <StartedEndpoint>(b => { b.ToCreateInstance( endpointConfiguration => Task.FromResult(EndpointWithExternallyManagedContainer.Create(endpointConfiguration, serviceCollection)), startableEndpoint => { spyContainer = new SpyConainer(serviceCollection); return(startableEndpoint.Start(spyContainer)); }); b.When(e => e.SendLocal(new SomeMessage())); }) .Done(c => c.GotTheMessage) .Run(); var builder = new StringBuilder(); var coreComponents = spyContainer.RegisteredComponents.Values .OrderBy(c => c.Type.FullName) .ToList(); builder.AppendLine("----------- Used registrations (Find ways to stop accessing them)-----------"); foreach (var component in coreComponents.Where(c => c.WasResolved)) { builder.AppendLine(component.ToString()); } builder.AppendLine("----------- Registrations not used by the core, can be removed in next major if downstreams have been confirmed to not use it -----------"); foreach (var component in coreComponents.Where(c => !c.WasResolved)) { builder.AppendLine(component.ToString()); } Approver.Verify(builder.ToString()); }
static async Task Main() { Console.Title = "Samples.NServiceBus.Extensions.DependencyInjection"; var endpointConfiguration = new EndpointConfiguration("Sample"); endpointConfiguration.UseTransport <LearningTransport>(); #region ContainerConfiguration var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <MyService>(); serviceCollection.AddSingleton <MessageSenderService>(); var endpointWithExternallyManagedContainer = EndpointWithExternallyManagedContainer .Create(endpointConfiguration, serviceCollection); // if needed register the session serviceCollection.AddSingleton(p => endpointWithExternallyManagedContainer.MessageSession.Value); #endregion using (var serviceProvider = serviceCollection.BuildServiceProvider()) { var endpoint = await endpointWithExternallyManagedContainer.Start(serviceProvider) .ConfigureAwait(false); var senderService = serviceProvider.GetRequiredService <MessageSenderService>(); await senderService.SendMessage() .ConfigureAwait(false); Console.WriteLine("Press any key to exit"); Console.ReadKey(); await endpoint.Stop() .ConfigureAwait(false); } }
public static Configure NServiceBus(this Configure config, EndpointConfiguration endpointConfig) { IStartableEndpointWithExternallyManagedContainer startableEndpoint = null; { var settings = endpointConfig.GetSettings(); var conventions = endpointConfig.Conventions(); // set the configured endpoint name to the one NSB config was constructed with config.SetEndpointName(settings.Get <string>("NServiceBus.Routing.EndpointName")); conventions.DefiningCommandsAs(type => typeof(Messages.ICommand).IsAssignableFrom(type)); conventions.DefiningEventsAs(type => typeof(Messages.IEvent).IsAssignableFrom(type)); conventions.DefiningMessagesAs(type => typeof(Messages.IMessage).IsAssignableFrom(type)); endpointConfig.AssemblyScanner().ScanAppDomainAssemblies = true; endpointConfig.EnableCallbacks(); endpointConfig.EnableInstallers(); endpointConfig.UseSerialization <Internal.AggregatesSerializer>(); endpointConfig.EnableFeature <Feature>(); } config.RegistrationTasks.Add(c => { var container = c.Container; container.Register(factory => new Aggregates.Internal.DelayedRetry(factory.Resolve <IMetrics>(), factory.Resolve <IMessageDispatcher>()), Lifestyle.Singleton); container.Register <IEventMapper>((factory) => new EventMapper(factory.Resolve <IMessageMapper>()), Lifestyle.Singleton); container.Register <UnitOfWork.IDomain>((factory) => new NSBUnitOfWork(factory.Resolve <IRepositoryFactory>(), factory.Resolve <IEventFactory>(), factory.Resolve <IVersionRegistrar>()), Lifestyle.UnitOfWork); container.Register <IEventFactory>((factory) => new EventFactory(factory.Resolve <IMessageMapper>()), Lifestyle.Singleton); container.Register <IMessageDispatcher>((factory) => new Dispatcher(factory.Resolve <IMetrics>(), factory.Resolve <IMessageSerializer>(), factory.Resolve <IEventMapper>(), factory.Resolve <IVersionRegistrar>()), Lifestyle.Singleton); container.Register <IMessaging>((factory) => new NServiceBusMessaging(factory.Resolve <MessageHandlerRegistry>(), factory.Resolve <MessageMetadataRegistry>(), factory.Resolve <ReadOnlySettings>()), Lifestyle.Singleton); var settings = endpointConfig.GetSettings(); settings.Set("Retries", config.Retries); settings.Set("SlowAlertThreshold", config.SlowAlertThreshold); settings.Set("CommandDestination", config.CommandDestination); // Set immediate retries to 0 - we handle retries ourselves any message which throws should be sent to error queue endpointConfig.Recoverability().Immediate(x => { x.NumberOfRetries(0); }); endpointConfig.Recoverability().Delayed(x => { x.NumberOfRetries(0); }); endpointConfig.MakeInstanceUniquelyAddressable(c.UniqueAddress); endpointConfig.LimitMessageProcessingConcurrencyTo(c.ParallelMessages); // NSB doesn't have an endpoint name setter other than the constructor, hack it in settings.Set("NServiceBus.Routing.EndpointName", c.Endpoint); startableEndpoint = EndpointWithExternallyManagedContainer.Create(endpointConfig, new Internal.ContainerAdapter()); return(Task.CompletedTask); }); // Split creating the endpoint and starting the endpoint into 2 seperate jobs for certain (MICROSOFT) DI setup config.SetupTasks.Add((c) => { return(Aggregates.Bus.Start(startableEndpoint)); }); return(config); }