static async Task Main(string[] args) { Console.Title = "Worker"; var endpointConfiguration = new EndpointConfiguration("Worker"); endpointConfiguration.UseTransport <LearningTransport>(); var serviceCollection = new ServiceCollection(); serviceCollection.AddScoped <IDbRepository <Domain.RepositoryModels.Account>, DbRepository <Domain.RepositoryModels.Account> >(); serviceCollection.AddScoped <IDbRepository <PaymentNotificationBase>, DbRepository <PaymentNotificationBase> >(); serviceCollection.AddLogging(); var endpointWithExternallyManagedServiceProvider = EndpointWithExternallyManagedServiceProvider .Create(endpointConfiguration, serviceCollection); using (var serviceProvider = serviceCollection.BuildServiceProvider()) { var endpointInstance = await endpointWithExternallyManagedServiceProvider.Start(serviceProvider) .ConfigureAwait(false); Console.WriteLine("Press Enter to exit."); Console.ReadLine(); await endpointInstance.Stop() .ConfigureAwait(false); } }
public NserviceBusEndpoint(string endPointName, IServiceCollection services) { endpointConfiguration = new EndpointConfiguration(endPointName); endpointConfiguration.UseTransport <LearningTransport>(); startableEndPoint = EndpointWithExternallyManagedServiceProvider .Create(endpointConfiguration, services); }
public async Task Should_resolve_dependencies_from_serviceprovider() { IUnityContainer container = null; IServiceProvider serviceProvider = null; var context = await Scenario.Define <Context>() .WithEndpoint <EndpointWithExternallyManagedContainer>(endpointBuilder => { var serviceCollection = new ServiceCollection(); // register service using the IServiceCollection API: serviceCollection.AddSingleton <ServiceCollectionService>(); // register service using the NServiceBus container API: endpointBuilder.CustomConfig(endpointConfiguration => endpointConfiguration .RegisterComponents(c => c .RegisterSingleton(new InternalApiService()))); endpointBuilder.ToCreateInstance( configuration => Task.FromResult(EndpointWithExternallyManagedServiceProvider .Create(configuration, serviceCollection)), startableEndpoint => { IServiceProviderFactory <IUnityContainer> factory = new ServiceProviderFactory(container); container = factory.CreateBuilder(serviceCollection); // register service using the container native API: container.RegisterSingleton <NativeApiService>(); serviceProvider = factory.CreateServiceProvider(container); return(startableEndpoint.Start(serviceProvider)); }); endpointBuilder.When(session => session.SendLocal(new TestMessage())); }) .Done(c => c.InjectedInternalApiService != null) .Run(); Assert.AreSame(context.InjectedNativeApiService, container.Resolve <NativeApiService>()); Assert.AreSame(context.InjectedNativeApiService, serviceProvider.GetService <NativeApiService>()); Assert.AreSame(context.InjectedInternalApiService, container.Resolve <InternalApiService>()); Assert.AreSame(context.InjectedInternalApiService, serviceProvider.GetService <InternalApiService>()); Assert.AreSame(context.InjectedServiceCollectionService, container.Resolve <ServiceCollectionService>()); Assert.AreSame(context.InjectedServiceCollectionService, serviceProvider.GetService <ServiceCollectionService>()); }
async Task ExternallyManagedMode(EndpointConfiguration endpointConfiguration) { #region externally-managed-mode var serviceCollection = new ServiceCollection(); serviceCollection.AddTransient <MyService>(); var startableEndpoint = EndpointWithExternallyManagedServiceProvider.Create(endpointConfiguration, serviceCollection); var serviceProvider = serviceCollection.BuildServiceProvider(); var endpoint = await startableEndpoint.Start(serviceProvider); serviceProvider.GetService <MyService>(); #endregion }
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 endpointWithExternallyManagedServiceProvider = EndpointWithExternallyManagedServiceProvider .Create(endpointConfiguration, serviceCollection); // if needed register the session serviceCollection.AddSingleton(p => endpointWithExternallyManagedServiceProvider.MessageSession.Value); #endregion using (var serviceProvider = serviceCollection.BuildServiceProvider()) { var endpoint = await endpointWithExternallyManagedServiceProvider.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); } }
static async Task Main(string[] args) { var defaultFactory = LogManager.Use <DefaultFactory>(); defaultFactory.Level(LogLevel.Debug); var services = new ServiceCollection(); // Create 'regular' endpoint ignoring all handlers in the priority assembly var regularEndpointConfiguration = new EndpointConfiguration("SomeService"); regularEndpointConfiguration.EnableInstallers(); regularEndpointConfiguration.UseTransport <RabbitMQTransport>() .UseConventionalRoutingTopology() .ConnectionString("host=localhost;username=guest;password=guest;virtualhost=testvhost;RequestedHeartbeat=600"); regularEndpointConfiguration.AssemblyScanner().ExcludeAssemblies(typeof(SomePriorityEventHandler).Assembly.GetName().Name); var regularStarableEndpoint = EndpointWithExternallyManagedServiceProvider.Create(regularEndpointConfiguration, services); // Create 'priority' endpoint ignoring all handlers in the regular assembly var priorityEndpointConfiguration = new EndpointConfiguration("SomeService.Priority"); priorityEndpointConfiguration.EnableInstallers(); priorityEndpointConfiguration.UseTransport <RabbitMQTransport>() .UseConventionalRoutingTopology() .ConnectionString("host=localhost;username=guest;password=guest;virtualhost=testvhost;RequestedHeartbeat=600"); priorityEndpointConfiguration.AssemblyScanner().ExcludeAssemblies(typeof(SomeRegularEventHandler).Assembly.GetName().Name); var priorityStartableEndpoint = EndpointWithExternallyManagedServiceProvider.Create(priorityEndpointConfiguration, services); var provider = services.BuildServiceProvider(); var regularEndpoint = await regularStarableEndpoint.Start(provider); var priorityEndpoint = await priorityStartableEndpoint.Start(provider); ConsoleWriter.WriteRedLine("Hit enter to send two events via each endpoint!"); Console.ReadLine(); ConsoleWriter.WriteGreenLine("Publishing two events from Regular Endpoint"); await regularEndpoint.Publish(new SomeRegularEvent()); await Task.Delay(TimeSpan.FromSeconds(5)); await regularEndpoint.Publish(new SomePriorityEvent()); await Task.Delay(TimeSpan.FromSeconds(5)); ConsoleWriter.WriteGreenLine("Publishing two events from Priority Endpoint"); await priorityEndpoint.Publish(new SomeRegularEvent()); await Task.Delay(TimeSpan.FromSeconds(5)); await priorityEndpoint.Publish(new SomePriorityEvent()); // Wait a sec to process the messages await Task.Delay(TimeSpan.FromSeconds(1)); await regularEndpoint.Stop(); await priorityEndpoint.Stop(); ConsoleWriter.WriteRedLine("Well done!"); Console.ReadLine(); }