1

I use ASP.NET Core 2.2 I am trying to call a basic service class from Startup. It is throwing this exception:

InvalidOperationException: Unable to resolve service for type 'TIR.NetCore.ICommonLogService' while attempting to activate 'AdminCentral.NetCore.Startup'.

This my code:

public class Startup { private readonly ICommonLogService _CommonLogService; public Startup(IConfiguration configuration, ICommonLogService CommonLogService) { _CommonLogService = CommonLogService; Configuration = configuration; } public IConfiguration Configuration { get; } public string connectionString; public IServiceProvider ConfigureServices(IServiceCollection services) { var container = new Container(); container.Configure(config => { config.AddRegistry(new StructuremapRegistry()); config.Populate(services); }); return container.GetInstance<IServiceProvider>(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { LogException(Exception ) } private void LogException(Exception error, HttpContext context) { _CommonLogService.InsertLogDetail(); } } 
2
  • Startup is used to configure your services. You cannot pass types that haven't been registered yet to the Startup constructor itself. You can use IHostingEnvironment, IConfiguration and ILoggerFactory but the framework doesn't know about ICommonLogService. Commented Mar 11, 2019 at 7:52
  • What class should implement ICommonLogService? I don't see the point in injecting it into the Startup class, but most likely you should instantiate it right there, as it is the entry point of the application. You usually inject code because you need to unit test the class which gets the injected code, but in this case, I really doubt that you will unit test the Startup. In other words, just use the concrete type to create a new Logger instance. Commented Mar 11, 2019 at 9:24

1 Answer 1

1

If you want to use ICommonLogService in the Startup.cs class, you need to get an instance from the container like this:

public IServiceProvider ConfigureServices(IServiceCollection services) { var container = new Container(); container.Configure(config => { config.AddRegistry(new StructuremapRegistry()); config.Populate(services); }); //Get an instance of ICommonLogService from container ICommonLogService CommonLogService = container.GetInstance<ICommonLogService>(); //Use CommonLogService here return container.GetInstance<IServiceProvider>(); } 
Sign up to request clarification or add additional context in comments.

1 Comment

thanks update post. how call method from ICommonLogService

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.