3

In an ASP.NET Core 1.0 project, using DI how can I pass parameters to constructor. For instance, how do I register the following service in Startup.cs. services.AddTransient(typeof(IStateService), new StateService()); does not work since StateService() requires an input parameter of type BlogingContext. Or, are there alternative way of building the following service with database involved? Here State is a table coming from SQL Server Db. App is using EntityFrameworkCore with Code First approach. I'm using latest release of ASP.NET Core 1.0 and VS2015-Update 3 released on June 27, 2016

I see a similar example here but not quite the same type of input parameter.

Service:

    public interface IStateService
    {
        IEnumerable<State> List();
    }

     public class StateService : IStateService
     {
         private BloggingContext _context;

         public StateService(BloggingContext context)
         {
             _context = context;
         }

         public IEnumerable<State> List()
         {
             return _context.States.ToList();
         }
     }
Nkosi
  • 235,767
  • 35
  • 427
  • 472
nam
  • 21,967
  • 37
  • 158
  • 332
  • Have you tried registering `BloggingContext` with something like `services.AddDbContext();` in startup? https://docs.asp.net/en/latest/fundamentals/dependency-injection.html#registering-your-own-services – mollwe Jul 03 '16 at 23:33
  • @mollwe Thank you for trying to help. Your code would not register my custom service i.e. `StateService` – nam Jul 03 '16 at 23:36
  • If you register state service as `service.AddTransient();`, will that make it work? – mollwe Jul 03 '16 at 23:40
  • @mollwe But the StateService() constructor needs a parameter of type BloggingContext. Thtat's what VS complains. – nam Jul 04 '16 at 00:10
  • But if you add both db context and the service as stated in my two comments the DI should resolve it I think, I haven't tested it though but that's how others work, e.g. Autofac. – mollwe Jul 04 '16 at 00:14
  • @mollwe VS still complains you need to pass a parameter of type BloggingContext to StateService(). I think, something similar to [this](http://stackoverflow.com/a/34834352/1232087) except that the parameter can't be hard coded in my case. – nam Jul 04 '16 at 02:26
  • That is strange. If I look at the documentation I sent you as a link in first comment it shows a similar example with DbContext and a service and there they do it as I described. Is it at run-time or build-time it complains? – mollwe Jul 04 '16 at 02:34
  • @mollwe It's a built-time complain. The doc you provided does not include an example of a custom service class that has a constructor with an input parameter like mine does. – nam Jul 04 '16 at 02:42
  • 1
    There is a CharacterController with a ICharacterRepository with a ApplicationDbContext if you scroll down a bit. Even further down they register them for DI. ICharacterRepository matches your IStateService and ApplicationDbContext matches your BloggingContext... – mollwe Jul 04 '16 at 02:46
  • @mollwe You're correct. Do you want to write your solution as a an answer and I'll accept that as an answer. And thank you for helping. – nam Jul 04 '16 at 02:53

1 Answers1

2

As documentation states here (Scroll a bit down) you should register the IStateService and BloggingContext like:

services.AddDbContext<BloggingContext>();
services.AddScoped<IStateService, StateService>();

Then DI will resolve the whole dependency tree for you. Note that you should use scoped lifetime on service, because the service should use same lifetime as DbContext and it uses scoped.

mollwe
  • 2,185
  • 2
  • 18
  • 17