2

I am trying to use the new ASP.NET 5 dependency injection system, but it seems limited to ONLY constructors of classes that inherit from Controller.

Is there any other way to inject things? Properties? Anything? This is so severely limiting and has had me brickwalling for days.

12
  • that is not true at all, any class with a constructor can easily registered with DI and injected wherever they are needed so long as all the types needed by the constructor are also registered in DI. each dependency in the chain must be registered with DI. what makes you think it is limited to controllers? Jan 5, 2016 at 18:18
  • 1) I can't find any examples otherwise. 2) I don't understand how the class gets instantiated then. What creates it?
    – Ciel
    Jan 5, 2016 at 18:23
  • I would be greatly in your debt if you could show me an example of how this works on other classes. I'm just not "getting" it. It is driving me up a wall of crazy.
    – Ciel
    Jan 5, 2016 at 18:25
  • The major thing I can't figure out is how another class could get created.
    – Ciel
    Jan 5, 2016 at 18:40
  • 1
    I think if you read question and answer here as well as the discussion in comments it will help you Jan 5, 2016 at 18:54

2 Answers 2

1

Just tested this (RC1 Update1), it works with other classes as well. I wrote a small example, first the type declarations:

public interface IBaseServiceType { }

public interface IComposedServiceType
{
    IBaseServiceType baseService { get; }
}

public class BaseServiceImplementation : IBaseServiceType { }

public class ComposedServiceImplementation : IComposedServiceType
{
    public IBaseServiceType baseService { private set; get; }

    public ComposedServiceImplementation(IBaseServiceType baseService)
    {
        this.baseService = baseService;
    }
}

The configuration:

        services.AddTransient(typeof(IBaseServiceType), typeof(BaseServiceImplementation));
        services.AddTransient(typeof(IComposedServiceType), typeof(ComposedServiceImplementation));

And create the instance like this where context is your HttpContext:

 var composedServiceInstance = context.ApplicationServices.GetService<IComposedServiceType>();
10
  • Is it possible to get it even if you don't have an HttpContext?
    – Ciel
    Jan 5, 2016 at 19:58
  • @Ciel: sure, you only need a reference to the IServiceProvider instance. Jan 5, 2016 at 20:06
  • Hrnm.. I'm trying to think of how to get that. Basically, I have a class that needs to run, but it isn't inside a Controller and it doesn't get an HttpContext at all.
    – Ciel
    Jan 5, 2016 at 20:07
  • 1
    you will have to make a decision when your "class needs to run". Jan 5, 2016 at 20:09
  • @Ciel: basically you have two options to pass data: using the stack or static members. static members can be accessed without an instance. Jan 5, 2016 at 23:05
0

Register your class as a service and treat it like you would all other services

see Net Core Dependency Injection for Non-Controller

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.