HttpContext is null in console app

Anonymous 1 Reputation point

I am trying to run multiple projects one is web app and other is console app. When I am trying to access http context in console app it is throwing null exception

Windows 10 Network
Windows 10 Network
Windows 10: A Microsoft operating system that runs on personal computers and tablets.Network: A group of devices that communicate either wirelessly or via a physical connection.
2,267 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 39,341 Reputation points

    Helllo @AparnaKochar

    System.Web.HttpContext.Current is populated by IIS which is not present/active during unit testing. Hence null. Do not tightly couple your code to HttpContext for that very reason. Instead encapsulate that behind abstractions that can be mocked when testing in isolation.

    Bad design aside, for your specific design you are trying to access a static dependency external to the controller. If faking the controller's context as demonstrated in your current test, then access the controller's context instead of calling the static context.

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments