locked
web api does have IsReusable feature like a generic handler RRS feed

  • Question

  • User2040514273 posted

    Hi,

    Generic handlers has a property called IsReusable (boolean), which when set to true can decrease overhead by using the same handle instance by several different requests.

    From what I have seen ASP.NET Web API does not offer a property like IsReusable, is that feature not supported anymore? does Web API already reusing the handler instance without me having to tell it to do so? if not, is there another way to tell Web API to reuse handler instances?

    Thanks a lot.

    Tuesday, September 22, 2015 5:13 AM

Answers

  • User2040514273 posted

    Thanks Li Wang, that's the answer I was looking for. 

    With that in mind, Does that mean that a web service which was implemented by ASP.NET Web API has more overhead on the server side than a web service which was implemented by IHttpHandler? (assuming both has a lot of traffic with the same amount of requests per second).

    If you could clarify on my query above i'll appreciate it.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 23, 2015 4:33 AM

All replies

  • User-488622176 posted

    As far as I know, this feature does not exist in Web API, as it would not serve any purpose. What do you want to do? Why should it be reusable?

    Tuesday, September 22, 2015 7:20 AM
  • User2040514273 posted

    Illeris,

    like I said, the IsReusable made it possible for asp.net to not create another instance of the handler's class type on subsequent requests to the same handler. Thus saving overhead.

    That is a good thing (-:

    Tuesday, September 22, 2015 9:47 AM
  • Tuesday, September 22, 2015 11:06 AM
  • User-488622176 posted

    that is not a good thing. You try to convert something that is designed to be stateless to something that is statefull. It has not been made for that purpose. New call = new thread. Everything else is in the core architecture of ASP.NET MVC/WebAPI. 

    If you have specific issues or questions related to this, feel free to post them. I do not know any scenario's where this behavior imposes problems :-)

    But you can always rewrite ASP.NET WebAPI :-). Actually if you want a controller to remain "active" for all calls, you could use the IOC functionality by overriding the IHttpControllerSelector and IHttpControllerActivator classes. See http://www.asp.net/web-api/overview/advanced/configuring-aspnet-web-api 
    Instead of creating a new controller instance for every request, you can cache the instance and use it for all requests. But beware: you can screw up easily. Do not do this unless you know what & why ;-)

    Tuesday, September 22, 2015 11:18 AM
  • User2040514273 posted

    Illeris,

    By making a class stay in memory just so I don't have to create instance of it does not necessary say statefull:

    if I don't store data outside of the ProcessRequest method (fields, properties) in the handler everything is stateless.

    This feature just helps to reuse a class instance, that's all.

    Tuesday, September 22, 2015 1:29 PM
  • User-84896714 posted

    Hi ido,

    According to the document of Lifecycle of an ASP.NET Web API . A web api message is handled by HttpMessageHandler instance. HttpMessageHandler doesn't implement IHttpHandler interface, it doesn't provide IsReusable property.

    Best Regards,
    Li Wang

    Tuesday, September 22, 2015 10:28 PM
  • User2040514273 posted

    Thanks Li Wang, that's the answer I was looking for. 

    With that in mind, Does that mean that a web service which was implemented by ASP.NET Web API has more overhead on the server side than a web service which was implemented by IHttpHandler? (assuming both has a lot of traffic with the same amount of requests per second).

    If you could clarify on my query above i'll appreciate it.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 23, 2015 4:33 AM
  • User2040514273 posted

    Here is a benchmark test, clearly Httphandler is faster than Web API:

    https://weblog.west-wind.com/posts/2012/Sep/04/ASPNET-Frameworks-and-Raw-Throughput-Performance

    Wednesday, September 23, 2015 11:30 AM
  • User-84896714 posted

    Hi ido,

    Httphandler is faster than Web API:

    Yes, Pure httphandler is faster than web api.  ASP.NET Web API is used for creating rich REST-ful Web Services that return JSON, XML, or any kind of content the web supports. It can improve programming efficiency. We must find a balance between codes execution  efficiency, programming efficiency and code scalability.

    Best Regards,
    Li Wang

    Wednesday, September 23, 2015 9:12 PM
  • User2040514273 posted

    Li Wang,

    I think you are right.

    Thanks.

    Thursday, September 24, 2015 12:23 PM