locked
Consuming web service by client RRS feed

  • Question

  • User-1129879462 posted

    I have a controller with a bunch of methods.

    I have a dashboard page which calls (Ajax) these methods, when each of the method is called an instance of controller is created and then the method is called. this results in lots of round trip.

    I am thinking something like this:

    Controller c=new Controller();

    c.method1();

    c.method2();

    How could I achieve this? (all calls made on a single instance of the controller)

    Thank you.

    Friday, February 24, 2017 9:22 PM

Answers

  • User753101303 posted

    Hi,

    It likely means that you actually have toi change your Ajax API as you find possible to call multiple controller methods in a single go.

    If you never get a result without getting some other result you should likely return them as part of a single Ajax call. If doing multiple calls to get the same kind of items you should likely have an API call that would allow to get a list of items.

    Thjis way you'll need less API calls to retrieve the same data (ie chunky over chatty API).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 25, 2017 9:18 AM

All replies

  • User527778624 posted

    Hi,

    AFAIK, Each request is convert to a thread which can work on application, here new controller instance is created every time.

    Even if you create an instance manually, you may not be able to reuse it. 

    Anyway check the code in this post:

    https://forums.asp.net/t/2036738.aspx?calling+an+MVC+controller+from+c+class

    a better option might be using websockets.

    Saturday, February 25, 2017 7:23 AM
  • User753101303 posted

    Hi,

    It likely means that you actually have toi change your Ajax API as you find possible to call multiple controller methods in a single go.

    If you never get a result without getting some other result you should likely return them as part of a single Ajax call. If doing multiple calls to get the same kind of items you should likely have an API call that would allow to get a list of items.

    Thjis way you'll need less API calls to retrieve the same data (ie chunky over chatty API).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 25, 2017 9:18 AM
  • User-2057865890 posted

    Hi krisrajz,

    ApiController's are instantiated by WebApi per request, so we can't very well create it once and reuse it. Moreover, there may be many running concurrently in response to multiple requests, and clearly they must not share state.

    reference: http://stackoverflow.com/a/25175814

    Best Regards,

    Chris

    Thursday, March 2, 2017 3:04 AM