ASP.net Web API: Why are controllers created per request?

前端 未结 2 1900
暖寄归人
暖寄归人 2020-12-08 03:09

Mike Wasson\'s article \"Dependency Injection for Web API Controllers\" on www.asp.net says:

Dependenecy Scope and Controller Lifetime

相关标签:
2条回答
  • 2020-12-08 03:50

    A controller contains information (state) about the incoming request.

    If you had only one controller to handle many requests then they would all be confused and users would likely get some strange results.

    0 讨论(0)
  • 2020-12-08 03:56

    If it wasn't recreated each request you'd effectively have a singleton or static class meaning you'd need to handle resetting the classes state on exceptions and all sorts of other cases. The result would almost certainly mean bugs.

    The overhead for creating a new context each time is a small price to pay for better code maintainability.

    0 讨论(0)
提交回复
热议问题