Skip to content

Instantly share code, notes, and snippets.

@ritasker
Created November 28, 2016 12:19
Show Gist options
  • Save ritasker/39ab6b1877565727e82b7d8f283dbf59 to your computer and use it in GitHub Desktop.
Save ritasker/39ab6b1877565727e82b7d8f283dbf59 to your computer and use it in GitHub Desktop.
using System;
using IoCTest.Modules;
using RawRabbit;
namespace IoCTest
{
public interface IMessagingClient : IDisposable
{
void Shutdown();
void Publish(BasicMessage basicMessage);
}
public class MessagingClient : IMessagingClient
{
private readonly IBusClient _client;
public MessagingClient(IBusClient client)
{
_client = client;
}
public void Shutdown()
{
Console.WriteLine("Shutdown....");
_client.ShutdownAsync().Wait();
}
public void Publish(BasicMessage basicMessage)
{
_client.PublishAsync(basicMessage);
}
public void Dispose()
{
Shutdown();
}
}
}
using Autofac;
using Nancy.Bootstrappers.Autofac;
using RawRabbit;
using RawRabbit.vNext;
namespace IoCTest
{
public class MyBootstrapper : AutofacNancyBootstrapper
{
protected override void ConfigureApplicationContainer(ILifetimeScope existingContainer)
{
var builder = new ContainerBuilder();
IBusClient busClient = BusClientFactory.CreateDefault();
builder.RegisterInstance(busClient).SingleInstance();
builder.RegisterType<MessagingClient>().As<IMessagingClient>().SingleInstance();
builder.Update(existingContainer.ComponentRegistry);
}
}
}
using System;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace IoCTest
{
internal class Program
{
public static void Main(string[] args)
{
using (var host = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel()
.UseStartup<Startup>()
.Build())
{
Console.WriteLine("Starting application on url : http://localhost:5000");
host.Start();
var appLifeTime = host.Services.GetRequiredService<IApplicationLifetime>();
appLifeTime.ApplicationStopping.Register(() =>
{
Console.WriteLine("Stopping application...");
});
Console.CancelKeyPress += (sender, e) =>
{
appLifeTime.StopApplication();
e.Cancel = true;
};
appLifeTime.ApplicationStopping.WaitHandle.WaitOne();
}
}
}
}
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;
using Nancy.Owin;
namespace IoCTest
{
public class Startup
{
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseOwin(x =>
{
x.UseNancy(options => options.Bootstrapper = new MyBootstrapper());
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment