-1

Our team has discovered, to our surprise, that our ApiController classes are instantiated for every incoming request. Most developers on our team expected that the controller instances would be reused across requests by default.*

We've searched for the justification or reasoning behind this, in order to understand the tradeoffs that might be involved between reusing Controller instances and creating new ones, but have have found nothing concrete. Is there a documented reference that talks about it?


*We do realize that there's probably a way to change the behavior (to make it reuse instances across requests), but that's not the meat of this question.

E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • I don't understand the close votes saying this is opinion-based - I specifically worded the question to ask for references, not opinions. – E-Riz Feb 16 '18 at 14:59
  • 2
    Your question derives from your lack of understanding how a process worked. You discovered for yourself how it actually works (kudos). But now you are asking for the justification of why the system was designed the way it was designed which is not a good SO question. (You asked for reference that "talk about it"... I'm assuming "it" meant "justification".) – Sailing Judo Feb 16 '18 at 15:21
  • @SailingJudo I did reword the question slightly to better reflect what I seek (design tradeoffs, etc). But you should not assume that I (we) don't understand how it works. In fact we do, quite well. I also fundamentally disagree that it's not "a good SO question." If you check my rep you'll find that I've written 600+ answers and dozens of questions, so I have a pretty decent understanding of how SO works and what makes a good or poor question. – E-Riz Feb 16 '18 at 15:38
  • Having said that, I'm trying to figure out how to word it differently to get the info I seek. It's not an opinion I want, rather the reasoning behind a particular design that has tradeoffs (presumably) on both sides. Other web app frameworks don't do it this way, so there must be some reasons and I'm hoping they are documented or at least "written about" somewhere. – E-Riz Feb 16 '18 at 15:40
  • @E-Riz An old question, but I have gone down this same rabbit hole myself. I suspect that the reasoning behind stating that your question is _"**primarily** opinion-based"_ (emphasis mine) is that unless you get a response or documentation from those who actually designed and/or wrote the code, any answer is necessarily opinion based. In general, questions of the form _"Why was {x} done this way?"_ do not seem to be well received on SO, especially more recently. – skomisa Oct 15 '19 at 20:06
  • 1
    @E-Riz I'm writing this comment just as food for thought. *REST by design is stateless* so instantiating for every request by default enforces this on the developers. Reusing instances by default allows devs to easily shoot themselves in the foot. – CodeAngry Dec 10 '19 at 18:41

1 Answers1

4

Sounds like you would like to learn more about how the Web API processes requests. A good place to start learning about the request pipeline is this visual overview.

ASP.NET Web API was released some time after the ASP.NET MVC framework was released. They both use a similar pipeline design. It makes sense that, at the beginning, they would have similar request handling approaches since they could be eventually merged into a single pipeline.

BTW: Most of the components of the pipeline can be replaced with custom versions. If you disagree with the ApiController implementation, simply replace the default IHttpControllerTypeResolver with one you roll yourself!

UPDATE: Web API Core has a totally different middleware design so none of this is applicable to that pipeline.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Sixto Saez
  • 12,610
  • 5
  • 43
  • 51
  • While all very interesting information, none of it really address the question. It does not even explain the fact that controller instances are created for every request, much less any justification or even a way to change that particular behavior. I'm not interested in changing how discovery of controllers or endpoints happens. – E-Riz Feb 16 '18 at 15:02
  • @E-Riz Nobody here is interested in justifying design decisions by Microsoft. However, if you come to the conclusion that you don't like the system, Sixto explained how to make it work the way you want. – Sailing Judo Feb 16 '18 at 15:28
  • The question did not ask _how_ to change it (even if it did, this answer doesn't explain anything about the particular issue the question is about), it asked about the tradeoffs in making it behave one way versus another. You (and others) think it's not a good question (I disagree obviously, but check my rep if you think I don't know what I'm talking about), but even so this is not a good answer _to the question that was asked_. – E-Riz Feb 16 '18 at 15:32