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(); } }
Startupis used to configure your services. You cannot pass types that haven't been registered yet to the Startup constructor itself. You can useIHostingEnvironment,IConfigurationandILoggerFactorybut the framework doesn't know aboutICommonLogService.ICommonLogService? I don't see the point in injecting it into theStartupclass, 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 theStartup. In other words, just use the concrete type to create a new Logger instance.