A day with .Net

My day to day experince in .net

Posts Tagged ‘bot ninject’

Dependency Injection in Microsoft Bot Framework

Posted by vivekcek on January 15, 2017

In this post i will show you, how you can implement dependency injection in Microsoft Bot application.
Basically Microsoft bot application is a Web API service. So you can implement dependency injection, the same way you done it in Web API.

Here i am using Ninject to implement dependency injection.

1. Add below Nuget Packages

“Ninject.Web.WebApi”

1

“Ninject.Web.Common.WebHost”

2

2. Go to App_Start folder and open NinjectWebCommon.cs

3. Update the CreateKernel() method by adding below line.

RegisterServices(kernel);
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
return kernel;

4. You may need to add below namespaces in NinjectWebCommon.cs

using System.Web.Http;
using Ninject.Web.WebApi;

5. Register your services in NinjectWebCommon.cs

private static void RegisterServices(IKernel kernel)
{
  kernel.Bind<IMyInterface>().To<MyClass>();
}    

6. Use this class inside your MessageController.

[BotAuthentication]
public class MessagesController : ApiController
{
        
private readonly IMyInterface instance;
public MessagesController(IMyInterface _instance)
{
  instance = _instance;
}

Posted in Bot Framework | Tagged: , , | Leave a Comment »