I have a simple .NET Core Web API application (it is a default WebAPI template).
Startup registration class is default:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
The controller is very simple also:
[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly IHttpContextAccessor _httpContextAccessor;
public ValuesController(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
[HttpGet]
public string Get()
{
return _httpContextAccessor.HttpContext.TraceIdentifier;
}
}
As you can see I have not registered IHttpContextAccessor
interface on Startup.
And ValuesController
works fine on Debug mode via Visual Studio 2017.
When I publish my application on IIS then I have exception.
The exception from Log file is here:
System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' while attempting to activate 'HttpAccessorTest.Controllers.ValuesController'
It is absolutely correct exception because I have not registered IHttpContextAccessor
as described here.
When I register IHttpContextAccessor
then all works fine of course.
So my question: Why IHttpContextAccessor
resolves without registration on VS2017 Debug mode?