示例#1
0
        public async Task SendEvents()
        {
            var services = new ServiceCollection();

            services.AddScoped <ServiceFactory>(p => p.GetService);

            services.AddSingleton <IServiceBus, MediaRBasedServiceBus>();

            services.AddTransient <INotificationHandler <Pinged> >(sp => new SyncPingedHandler(testConsole, "Sync no error", false));
            services.AddTransient <INotificationHandler <Pinged> >(sp => new AsyncPingedHandler(testConsole, "async no error", false));
            services.AddTransient <INotificationHandler <Pinged> >(sp => new AsyncPingedHandler(testConsole, "async with error", true));
            services.AddTransient <INotificationHandler <Pinged> >(sp => new SyncPingedHandler(testConsole, "sync with error", true));

            var provider = services.BuildServiceProvider();

            var publisher = provider.GetRequiredService <IServiceBus>();

            var pinged = new Pinged();

            await publisher.Publish(pinged);

            Assert.Empty(pinged.Pongs);

            await Task.Delay(300);

            Assert.Equal(2, pinged.Pongs.Count);
        }
示例#2
0
        static async Task Main(string[] args)
        {
            var services = new ServiceCollection();

            services.AddScoped <ServiceFactory>(p => p.GetService);

            services.AddSingleton <Publisher>();

            services.AddTransient <INotificationHandler <Pinged> >(sp => new SyncPingedHandler("1"));
            services.AddTransient <INotificationHandler <Pinged> >(sp => new AsyncPingedHandler("2"));
            services.AddTransient <INotificationHandler <Pinged> >(sp => new AsyncPingedHandler("3"));
            services.AddTransient <INotificationHandler <Pinged> >(sp => new SyncPingedHandler("4"));

            var provider = services.BuildServiceProvider();

            var publisher = provider.GetRequiredService <Publisher>();

            var pinged = new Pinged();

            foreach (PublishStrategy strategy in Enum.GetValues(typeof(PublishStrategy)))
            {
                Console.WriteLine($"Strategy: {strategy}");
                Console.WriteLine("----------");

                try
                {
                    await publisher.Publish(pinged, strategy);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"{ex.GetType()}: {ex.Message}");
                }

                await Task.Delay(1000);

                Console.WriteLine("----------");
            }

            Console.WriteLine("done");
        }
示例#3
0
            private void HandlePing(Ping ping)
            {
                Pinged.Complete(ping);

                ping.ResponseChannel.Send(new Pong());
            }
示例#4
0
 public static void PublishPingedEvent()
 {
     // GD.Print("Publshing new motor outputs");
     Pinged?.Invoke();
 }