Asp.Net Core 3.1 Dependency Injection Error Unable to resolve service for type 'Microsoft.AspNetCore.Http.HttpContext

How to Inject Custom Http Response Header Click to Watch Video

Error: Unable to resolve service for type 'Microsoft.AspNetCore.Http.HttpContext

Please Login to see the rest of the answer

Answer: There is a chance that you are trying to inject none Interface into a Class's Constructor. Remember, Dependency Injection works with Interfaces that have been registered with the Service Container in the Startup.cs Class file. When you try to inject HttpContext in the Constructor, the Error happens. For a solution to solve this problem see below:

  • Change HttpContext httpContext to IHttpContextAccessor contextAccessor in the Constructor of your Class, the error should be resolved.
  • Please leave a message below if this solution helped you, other users might want to see how you resolved the issue. Thank you. 

Write an Article
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.

md said:

Thank you bro.. it helped me..

© 2022 - ErnesTech - Privacy