Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chivandikwa/55b95ef13928ab3326841d79b0208cec to your computer and use it in GitHub Desktop.
Save chivandikwa/55b95ef13928ab3326841d79b0208cec to your computer and use it in GitHub Desktop.
Replace Microsoft.Extensions.DependencyInjection.IServiceCollection with SimpleInjector in Microsoft Bot Framework project
using CoreBot.Bots;
using CoreBot.Dialogs;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SimpleInjector;
namespace CoreBot
{
public class Startup
{
private readonly Container _container = new Container();
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// Wire up simple injector for aspnetcore with controller activation
// Sets up the basic configuration that allows Simple Injector to be used in frameworks that require
// the use of Microsoft.Extensions.DependencyInjection.IServiceCollection
services.AddSimpleInjector(_container, options =>
{
options.AddAspNetCore()
// Registers all application's controllers in Simple Injector and instructs ASP.NET Core to let
// Simple Injector create those controllers.
.AddControllerActivation();
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
// Finalizes the configuration of Simple Injector on top of Microsoft.Extensions.DependencyInjection.IServiceCollection. Will
// ensure framework components can be injected into Simple Injector-resolved components
app.UseSimpleInjector(_container, options =>
{
// Allows components that are built by Simple Injector to depend on the (non-generic)
// Microsoft.Extensions.Logging.ILogger abstraction. Components are
// injected with an contextual implementation. Using this method, application components can simply
// depend on ILogger instead of its generic counter part, ILogger<T>, which
// simplifies development.
options.UseLogging();
});
InitializeContainer();
_container.Verify();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc();
}
private void InitializeContainer()
{
_container.Register<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>(Lifestyle.Singleton);
_container.Register<IStorage>(() => new MemoryStorage(), Lifestyle.Singleton);
_container.Register<UserState>(Lifestyle.Singleton);
_container.Register<ConversationState>(Lifestyle.Singleton);
_container.Register<MainDialog>(Lifestyle.Singleton);
_container.Register<IBot, DialogAndWelcomeBot<MainDialog>>();
_container.Register<ICredentialProvider, ConfigurationCredentialProvider>(Lifestyle.Singleton);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment