0

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.

enter image description here

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?

Alexander I.
  • 2,380
  • 3
  • 17
  • 42
  • @mjwills `Microsoft.AspNetCore.Http.HttpContextAccessor`. Assembly is `Microsoft.AspNetCore.Http, Version=2.0.1.0` – Alexander I. Mar 28 '18 at 10:47
  • @mjwills It is not duplicate. My question: Why IHttpContextAccessor resolves without registration on VS2017 Debug mode? – Alexander I. Mar 28 '18 at 10:59
  • I suspect your IIS and your local machine may be running different versions of .NET Core. – mjwills Mar 28 '18 at 11:01
  • @mjwills My Target Framework is .NET Core 2.0 – Alexander I. Mar 28 '18 at 11:03
  • Did adding `services.TryAddSingleton();` solve your issue (locally and on IIS)? – mjwills Mar 28 '18 at 11:04
  • @mjwills Of cource `services.TryAddSingleton();` solves the exception trouble. But my question is: **Why IHttpContextAccessor can correctly resolves without registration on Debug mode**? I have not registered IHttpContextAccessor specially for tests on debug mode. And I can see the IHttpContextAccessor can resolves correctly without registration. – Alexander I. Mar 28 '18 at 11:13
  • @mjwills My question is not about how to fix exception. My question why I do not see exception on Debug mode. – Alexander I. Mar 28 '18 at 11:14
  • The duplicate link suggests that for some versions of .NET Core, registration is _automatic_. – mjwills Mar 28 '18 at 11:15
  • @mjwills you think my VS2017 use previous version of .NET Core ? How I can check it? My target framework is `.NET Core 2.0`. Also I have nuget package `Microsoft.AspNetCore.All (2.0.5)`. So I think on my VS use .NET Core 2.0 on debug mode. – Alexander I. Mar 28 '18 at 11:24

0 Answers0