1

I am using Simple Injector in an ASP.NET 5 app, and need to switch over my Identity registrations from the built in Microsoft DI.

Currently, it looks like this;

services.AddIdentity<AppUser, AppRole>(o => {
    o.Password.RequiredLength = 6;
    o.Password.RequireNonLetterOrDigit = false;
    o.Password.RequireDigit = true;
    o.Password.RequireLowercase = false;
    o.Password.RequireUppercase = false;
})
.AddUserStore<UserStore<AppUser>>()
.AddRoleStore<RoleStore<AppRole>>()
.AddDefaultTokenProviders();

I'm aware of how to use Container.Register<T>, but I'm having a hard time finding the equivalent way of registering these particular things in Simple Injector. Has anyone else done this that might be able to help?

I am trying to do this because I need to use property injection; I have a task that will be setup using Quartz.NET, and it instantiates things on its own, like this -

public class HelloJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        Console.WriteLine("Greetings from HelloJob!");
    }
}


// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<HelloJob>()
    .WithIdentity("job1", "group1")
    .Build();

// Trigger the job to run now, and then repeat every 10 seconds
ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("trigger1", "group1")
    .StartNow()
    .WithSimpleSchedule(x => x
        .WithIntervalInSeconds(10)
        .RepeatForever())
    .Build();

// Tell quartz to schedule the job using our trigger
scheduler.ScheduleJob(job, trigger);

I need to do something with the database inside of HelloJob.Execute, but since it is not a Controller (and even if it was, it wouldn't get activated like one), I cannot inject the database into the constructor.

Steven
  • 166,672
  • 24
  • 332
  • 435
Ciel
  • 4,290
  • 8
  • 51
  • 110
  • What do you want to register in Simple Injector and why? – Steven Dec 27 '15 at 17:55
  • I need to use property injection in a class that does not inherit from `Controller`. I have looked for days and this does not seem possible with the built-in dependency injection, so I have to explore a third party one. – Ciel Dec 27 '15 at 18:26
  • For some reason, Microsoft assumes the only time - ever - to use DI is in controllers. But I am trying to run some code with `Quartz.NET` and to do it, the `IJob` I created needs to get to the database. I can't think of any other way to get the database to it than property injection or a hard coded instantiate. So I'm trying SimpleInjector. – Ciel Dec 27 '15 at 18:34
  • True; property injection is not possible with the built-in DI container. But as a matter of fact, you should typically not use property injection. Constructor injection is the preferred and advised way of doing dependency injection. Can you update your question with more detailed information on why you think you need property injection. Besides, the advice is to let framework and third party registrations inside the ASP.NET configuration system, and only add cross-wired registrations to Simple Injector for registrations that your own components actually require directly. – Steven Dec 27 '15 at 19:34
  • Alright, I've updated it. – Ciel Dec 27 '15 at 20:03
  • man this stuff is frustrating. I can't discern if this is the same problem as my other question or not. I just want to use Simple Injector, and not mess with the built-in Microsoft DI. But it feels like that isn't possible at all. – Ciel Dec 27 '15 at 23:34
  • 1
    There are a lot of "guides" online showing how to do it, but almost all of them are for asp.net 5 beta5 or lower, and are no longer accurate. – Ciel Dec 27 '15 at 23:35

1 Answers1

2

Quartz.NET allows the creation of jobs to be intercepted and this allows you to either forward the creation to the ASP.NET configuration system or Simple Injector (whatever you like). I'm not very familiar with Quartz and how they now integrate with ASP.NET 5, but there's the concept of the IJobFactory. Forwarding the call is a matter of implementing the NewJob() method. For instance, here's an implementation for Simple Injector:

public class SimpleInjectorJobFactory : IJobFactory {
    private readonly Container container;
    public SimpleInjectorJobFactory(Container container) {
        this.container = container;
    }
    public IJob NewJob(TriggerFiredBundle bundle) {
        return (IJob)container.GetInstance(bundle.JobDetail.JobType);
    }
}

For ASP.NET 5, the implementation might look like this:

public class SimpleInjectorJobFactory : IJobFactory {
    private readonly IServiceProvider container;
    public SimpleInjectorJobFactory(IServiceProvider container) {
        this.container = container;
    }
    public IJob NewJob(TriggerFiredBundle bundle) {
        return (IJob)container.GetRequiredService(bundle.JobDetail.JobType);
    }
}

This way you can simply inject dependencies into the constructor of your HelloJob and you don't need to revert to property injection. Prevent doing property injection.

Here's complete example of how to integrate Quartz.NET with Simple Injector.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • This is very good information and while it is what I ultimately have to do, that still doesn't solve the problem of being unable to get SimpleInjector to run under ASP.NET 5. At least I don't think so, unless I missed something you told me. – Ciel Dec 29 '15 at 23:17
  • @Ciel You might want to ask at the Quartz.NET forum (or hope the Quartz guys answer here). I'm not familiar enough with how Quartz integrates with ASP.NET 5 to help you on this. – Steven Dec 29 '15 at 23:34