3

I am injecting dependencies in my controller as follows:

private IEmailSender _emailSender;

public HomeController(IEmailSender emailSender)
{
     _emailSender = emailSender;
}

Then resolving the dependencies using Ninject as follows:

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind(typeof(IEmailSender)).To(typeof(EmailSender));   
}

So far everything is working fine. But I need to use this IEmailSender service in Startup class.

What I have tried so far is:

public partial class Startup
{
    private IEmailSender _emailSender;

    public Startup(IEmailSender emailSender)
    {
        _emailSender = emailSender;
    }

    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);

       _emailSender.SendEmail();
    }
}

But this does not work as application does not start because Startup class does not have parameter less constructor.

Any idea how I can use IEmailSender in Startup class?

8
  • Not ideal, but you might be able to do this using Property Setter Injection - replace private IEmailSender _emailSender; with [Inject]public IEmailSender _emailSender { private get; set; } and remove the constructor Commented Oct 6, 2018 at 3:34
  • @StephenMuecke Does not work! Object reference not set to an instance of an object. Commented Oct 6, 2018 at 4:56
  • Is Startup() begin called before call to initialize Ninject in global.asax.cs? Commented Oct 6, 2018 at 4:59
  • 1
    Another option might be IEmailSender _emailSender = DependencyResolver.Current.GetService<IEmailSender>(); Commented Oct 6, 2018 at 5:02
  • @StephenMuecke This is creating the instance as expected bu the problem is that asyncmethod is not working as expected in the Configuration(IAppBuilder app). Have you any thought regarding this please? Commented Oct 6, 2018 at 5:40

2 Answers 2

4

Ninject assigns itself as the current DependencyResolver instance, so you can use its .GetService() method to inject the concrete instance

public partial class Startup
{
    private IEmailSender _emailSender;
    public Startup()
    {
        _emailSender = DependencyResolver.Current.GetService<IEmailSender>();
    }
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
       _emailSender.SendEmail();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can create the kernel in Program.cs main function. So all the objects and services are registered before Startup.cs is called. This way ninject will able to resolve IEmailSender in startup class.

2 Comments

There is no Program.cs in a mvc web app
ok, my bad. then dont inject in ctor. use kernel.Get<IEmailSender>() to get an email sender instance. public void Configuration(IAppBuilder app) { ConfigureAuth(app); var emailsender = kernel.Get<IEmailSender>(); emailSender.SendEmail(); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.