Exemple #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            var endpointConfiguration = new NServiceBus.EndpointConfiguration("demo");
            var transport             = endpointConfiguration.UseTransport <RabbitMQTransport>();

            transport.ConnectionString(@"host=localhost;username=guest;password=guest");
            transport.UseConventionalRoutingTopology();
            transport.Routing().RouteToEndpoint(typeof(HelloWorldMessage), "consumer1");
            endpointConfiguration.SendOnly();
            endpointConfiguration.UseSerialization <NewtonsoftSerializer>();
            endpointConfiguration.EnableInstallers();
            var endpointInstance = Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();

            services.AddSingleton <IMessageSession>(endpointInstance);
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info {
                    Title = "My API", Version = "v1"
                });
            });
            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });
        }
        //create messaging endpoint
        private static async Task Main()
        {
            Console.Title = "ClientUI";

            //EndpointConfiguration class defintes all settings which determines how endpoint operates
            //string ClientUI is endpoint name
            var endpointConfiguration = new EndpointConfiguration("ClientUI");

            //            //transport is a setting which NServiceBus uses to send and receives messages
            //            var transport = endpointConfiguration.UseTransport<LearningTransport>();

            //MSMQ does not support natively Publish/Subscribe
            var transport = endpointConfiguration.UseTransport <MsmqTransport>();

            //message subscription info stored in memory instead
            endpointConfiguration.UsePersistence <InMemoryPersistence>();

            //error queue specified
            endpointConfiguration.SendFailedMessagesTo("error");

            //creates message queues required by endpoints
            endpointConfiguration.EnableInstallers();

            //establises commands of type PlaceOrder should be sent to the Sales endpoint
            var routing = transport.Routing();

            routing.RouteToEndpoint(typeof(PlaceOrder), "Sales");

            endpointConfiguration.UseSerialization <JsonSerializer>();

            //starts endpoint
            var endpointInstance = await Endpoint.Start(endpointConfiguration)
                                   .ConfigureAwait(false);

            await RunLoop(endpointInstance)
            .ConfigureAwait(false);

            //endpoint runs until user presses enter and then stops
            await endpointInstance.Stop()
            .ConfigureAwait(false);
        }