2019-08-16 10:51:35 +02:00
|
|
|
|
using System;
|
2019-08-18 11:06:33 +02:00
|
|
|
|
using System.Collections.Generic;
|
2019-08-16 10:51:35 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2019-08-19 16:36:26 +02:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Castle.MicroKernel.Registration;
|
|
|
|
|
using Castle.Windsor;
|
2019-08-16 10:51:35 +02:00
|
|
|
|
using ExampleLib;
|
2019-08-19 16:36:26 +02:00
|
|
|
|
using Ninject;
|
2019-08-16 10:51:35 +02:00
|
|
|
|
using Tapeti;
|
2019-08-19 16:36:26 +02:00
|
|
|
|
using Tapeti.Autofac;
|
|
|
|
|
using Tapeti.CastleWindsor;
|
2019-08-16 11:47:57 +02:00
|
|
|
|
using Tapeti.DataAnnotations;
|
2019-08-16 10:51:35 +02:00
|
|
|
|
using Tapeti.Default;
|
2019-08-19 16:36:26 +02:00
|
|
|
|
using Tapeti.Ninject;
|
2019-08-16 10:51:35 +02:00
|
|
|
|
using Tapeti.SimpleInjector;
|
2019-08-19 16:36:26 +02:00
|
|
|
|
using Container = SimpleInjector.Container;
|
2019-08-16 10:51:35 +02:00
|
|
|
|
|
2021-05-29 21:51:58 +02:00
|
|
|
|
// ReSharper disable UnusedMember.Global
|
|
|
|
|
|
2019-08-16 10:51:35 +02:00
|
|
|
|
namespace _01_PublishSubscribe
|
|
|
|
|
{
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
2022-02-09 09:19:56 +01:00
|
|
|
|
public static void Main()
|
2019-08-16 10:51:35 +02:00
|
|
|
|
{
|
2019-08-19 16:36:26 +02:00
|
|
|
|
var dependencyResolver = GetSimpleInjectorDependencyResolver();
|
2019-08-16 10:51:35 +02:00
|
|
|
|
|
2019-08-19 16:36:26 +02:00
|
|
|
|
// or use your IoC container of choice:
|
|
|
|
|
//var dependencyResolver = GetAutofacDependencyResolver();
|
|
|
|
|
//var dependencyResolver = GetCastleWindsorDependencyResolver();
|
|
|
|
|
//var dependencyResolver = GetNinjectDependencyResolver();
|
2019-08-16 10:51:35 +02:00
|
|
|
|
|
|
|
|
|
// This helper is used because this example is not run as a service. You do not
|
|
|
|
|
// need it in your own applications.
|
|
|
|
|
var helper = new ExampleConsoleApp(dependencyResolver);
|
|
|
|
|
helper.Run(MainAsync);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal static async Task MainAsync(IDependencyResolver dependencyResolver, Func<Task> waitForDone)
|
|
|
|
|
{
|
|
|
|
|
var config = new TapetiConfig(dependencyResolver)
|
2019-08-16 11:47:57 +02:00
|
|
|
|
.WithDataAnnotations()
|
2019-08-16 10:51:35 +02:00
|
|
|
|
.RegisterAllControllers()
|
|
|
|
|
.Build();
|
|
|
|
|
|
2022-02-09 09:19:56 +01:00
|
|
|
|
await using var connection = new TapetiConnection(config)
|
2019-08-16 10:51:35 +02:00
|
|
|
|
{
|
|
|
|
|
// Params is optional if you want to use the defaults, but we'll set it
|
|
|
|
|
// explicitly for this example
|
|
|
|
|
Params = new TapetiConnectionParams
|
|
|
|
|
{
|
|
|
|
|
HostName = "localhost",
|
|
|
|
|
Username = "guest",
|
2019-08-18 11:06:33 +02:00
|
|
|
|
Password = "guest",
|
|
|
|
|
|
|
|
|
|
// These properties allow you to identify the connection in the RabbitMQ Management interface
|
|
|
|
|
ClientProperties = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "example", "01 - Publish Subscribe" }
|
|
|
|
|
}
|
2019-08-16 10:51:35 +02:00
|
|
|
|
}
|
2022-02-09 09:19:56 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// IoC containers that separate the builder from the resolver (Autofac) must be built after
|
|
|
|
|
// creating a TapetConnection, as it modifies the container by injecting IPublisher.
|
|
|
|
|
(dependencyResolver as AutofacDependencyResolver)?.Build();
|
2019-08-19 16:36:26 +02:00
|
|
|
|
|
|
|
|
|
|
2022-02-09 09:19:56 +01:00
|
|
|
|
// Create the queues and start consuming immediately.
|
|
|
|
|
// If you need to do some processing before processing messages, but after the
|
|
|
|
|
// queues have initialized, pass false as the startConsuming parameter and store
|
|
|
|
|
// the returned ISubscriber. Then call Resume on it later.
|
|
|
|
|
await connection.Subscribe();
|
2019-08-16 10:51:35 +02:00
|
|
|
|
|
|
|
|
|
|
2022-02-09 09:19:56 +01:00
|
|
|
|
// We could get an IPublisher from the container directly, but since you'll usually use
|
|
|
|
|
// it as an injected constructor parameter this shows
|
|
|
|
|
await dependencyResolver.Resolve<ExamplePublisher>().SendTestMessage();
|
2019-08-16 10:51:35 +02:00
|
|
|
|
|
|
|
|
|
|
2022-02-09 09:19:56 +01:00
|
|
|
|
// Wait for the controller to signal that the message has been received
|
|
|
|
|
await waitForDone();
|
2019-08-16 10:51:35 +02:00
|
|
|
|
}
|
2019-08-19 16:36:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal static IDependencyContainer GetSimpleInjectorDependencyResolver()
|
|
|
|
|
{
|
|
|
|
|
var container = new Container();
|
|
|
|
|
|
|
|
|
|
container.Register<ILogger, ConsoleLogger>();
|
|
|
|
|
container.Register<ExamplePublisher>();
|
|
|
|
|
|
|
|
|
|
return new SimpleInjectorDependencyResolver(container);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal static IDependencyContainer GetAutofacDependencyResolver()
|
|
|
|
|
{
|
|
|
|
|
var containerBuilder = new ContainerBuilder();
|
|
|
|
|
|
|
|
|
|
containerBuilder
|
|
|
|
|
.RegisterType<ConsoleLogger>()
|
|
|
|
|
.As<ILogger>();
|
|
|
|
|
|
|
|
|
|
containerBuilder
|
|
|
|
|
.RegisterType<ExamplePublisher>()
|
|
|
|
|
.AsSelf();
|
|
|
|
|
|
|
|
|
|
return new AutofacDependencyResolver(containerBuilder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal static IDependencyContainer GetCastleWindsorDependencyResolver()
|
|
|
|
|
{
|
|
|
|
|
var container = new WindsorContainer();
|
|
|
|
|
|
|
|
|
|
// This exact combination is registered by TapetiConfig when running in a console,
|
|
|
|
|
// and Windsor will throw an exception for that. This is specific to the WindsorDependencyResolver as it
|
|
|
|
|
// relies on the "first one wins" behaviour of Windsor and does not check the registrations.
|
|
|
|
|
//
|
|
|
|
|
// You can of course register another ILogger instead, like DevNullLogger.
|
|
|
|
|
//container.Register(Component.For<ILogger>().ImplementedBy<ConsoleLogger>());
|
|
|
|
|
|
|
|
|
|
container.Register(Component.For<ExamplePublisher>());
|
|
|
|
|
|
|
|
|
|
return new WindsorDependencyResolver(container);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal static IDependencyContainer GetNinjectDependencyResolver()
|
|
|
|
|
{
|
|
|
|
|
var kernel = new StandardKernel();
|
|
|
|
|
|
|
|
|
|
kernel.Bind<ILogger>().To<ConsoleLogger>();
|
|
|
|
|
kernel.Bind<ExamplePublisher>().ToSelf();
|
|
|
|
|
|
|
|
|
|
return new NinjectDependencyResolver(kernel);
|
|
|
|
|
}
|
2019-08-16 10:51:35 +02:00
|
|
|
|
}
|
|
|
|
|
}
|