0

I have two websites, one client website and a pricing WEBAPI website. I have a performance issue with the pricing website as it often suspends itself due to low usage and on the first call takes time to initialize. If I repeat the request immediately after that, it is very quick.

I know that it will be used on certain pages of the client website, I therefore wish to call it when that page loads so its ready when the users valid request comes in seconds later. Please note the pricing WEBAPI site is not available from the client, only the client website can access it on the server side.

I don't know the best approach to this, I don't wish to impact the performance of the client website. I have considered an 1px x 1px iFrame calling a page but concerned it will block the page. Is an Ajax call more appropriate, but how to I call something on the client website to in turn call the webservice? Is there a better approach I haven't considered?

Steve Newton
  • 1,046
  • 1
  • 11
  • 28

1 Answers1

0

Known issue on shared hosting environments, a workaround is fine but I would suggest upgrading your server. My hosting has a DotNetNuke option, which essentially means it will reserve memory on the server and don't recycle the app pool due inactivity. Compared to VPS this is cheaper.

If it is not shared hosting, these IIS settings could help you.

Anyway, back to your workaround:

You say the client cannot access the webapi, only the back-end of your website can. Seems weird because an webapi exposes REST GET,POST methods. Either way you could do an async call to your webapi on server side that does not wait for a response or do a javascript call to your API.

Assuming your website is also ASP.NET:

public static async Task StartupWebapi()
{
    string requestUrl = "http://yourwebapi.com/api/startup";

    using (var client = new HttpClient())
    {
        //client.Timeout = new TimeSpan(0, 0, 20); timeout if needed
        try
        {
            HttpResponseMessage response = await client.GetAsync(requestUrl);
            if (response.IsSuccessStatusCode)
            {
                resultString = await response.Content.ReadAsStringAsync();
            }
        }

    }
}

Then, somewhere in your code, that will be called at least when your client website starts.

HostingEnvironment.QueueBackgroundWorkItem(ct => SomeClass.StartupWebapi());

Or in javascript, which is executed asynchronously.

$.ajax({
    url: "http://yourwebapi.com/api/startup",
    type: "GET",
    success: function (response) {
    },
    error: function (response) {

    }
});

See this question for some other workarounds.

Community
  • 1
  • 1
CularBytes
  • 9,924
  • 8
  • 76
  • 101