2

I have the following Repository and Class;

public interface IValueService
{
    GetAll();
}

public class ValueService : IValueService
{
    private DataContext _context;

    public ValueService(DataContext context)
    {
        _context = context;            
    }

    public GetAll()
    {
       // do something
    }
}

In my Startup.cs;

services.AddScoped<IValueService, ValueService>();

How would i call the method GetAll() from Startup or from another Class?

EDIT: Answered in; .NET Core 2 - Create instance of controller class which has a repository

phicon
  • 3,549
  • 5
  • 32
  • 62
  • from another class ? Inject `IValueService` via constructor injection and use that. Take a look at [Dependency injection in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection) – Shyju Feb 06 '18 at 00:43
  • This is basially the same question you aked a few minutes ago https://stackoverflow.com/questions/48633396/net-core-2-create-instance-of-controller-class-which-has-a-repository – DavidG Feb 06 '18 at 00:45
  • There is nothing wrong in this question to receive down votes. If so, please share your critic so the author can improve the question. – Tiago B Feb 06 '18 at 01:02
  • @DavidG yes, still the same problem - would be great if could point me in the right direction. To my understanding a controller gets called from the front-end. However, i just need to know how to call these methods without using a controller / front-end. – phicon Feb 06 '18 at 01:02
  • 1
    You've already been given a few links, it's now up to you to go and learn. – DavidG Feb 06 '18 at 01:03
  • @phicon, you don't have to use controller to call a service. Service A could call Service B as well. Just follow the link I put in my answer and you will learn that. – NonStatic Feb 06 '18 at 01:09

3 Answers3

3

To call GetAll(), you need to have an instance first, no matter where. So if you want to call it in the Startup.cs, you have to create an object first and then call it.

For other class to call the GetAll(), you need to specify IValueService as one of constructor's parameter, then in the constructor, you keep the IValueService instance in a local private property. This private property could be used in the other methods in the same class.

For .NetCore 2 dependency injection, click the MSDN Link for more details.

NonStatic
  • 951
  • 1
  • 8
  • 27
1

Could be this article can help: https://medium.com/@mattmazzola/asp-net-core-injecting-custom-data-classes-into-startup-classs-constructor-and-configure-method-7cc146f00afb

Basically to inject some service to the startup, you need to call ConfigureServices before calling UseStartup in WebHostBuilder() chain:

var host = new WebHostBuilder()
.ConfigureServices(servicesCollection =>
{
    servicesCollection.AddSingleton<IValueService>(new ValueService(context));
})
.UseStartup<Startup>()
.Build();

Then your service would be resolved if injected to startup constructor:

    public class Startup
    {
        public Startup(IConfiguration configuration, IHostingEnvironment env, IValueService service)
        {
           //service.GetAll();
        }
    }
twinmind
  • 1,440
  • 13
  • 21
0

In your controller or class you want to use add the following code:

public class MyController : Controller
{
    private readonly IValueService _valueServiceRepository;

    public MyController (IValueService _valueServiceRepository)
    {
        _valueServiceRepository = valueServiceRepository;
    }

}

Now you can use _valueServiceRepository.GetAll().

tonedblue
  • 164
  • 1
  • 6