Avoid client timeout on WebAPI call

If a WebAPI service takes a long time to complete, the caller, the client side, may timeout waiting for a response. There are several options to solve this issue, you can increase the timeout limit on the client side and web server side, or use a more persistent connection method like WebSocket and SignalR.

Here I propose a different way to avoid the timeout issue that allows the clients side to proactively check the result instead of passively waiting. The idea is pretty intuitive – since the client side times out on waiting for a response, why don’t just disconnect and check back the result later? So the solution is to disconnect with the client side as soon as the request is received and a WebAPI service is provided to enable client to check for the status of the processing. This way the client does not need to keep the connection open until the processing completes or times out. It can simply calls back every 5 seconds to check the result of the processing.

Here I am going to provide some sample code on how to achieve this. First, we create a Task class

public class Task
{
   public string TaskID;
   public Request Request;
   public Status Status;
   public Result Result;

   public void Execute(){
   // Do all sort of calculation and processing here
   // When done, sets the Status of the task to Finished.
   }
}

We then create a class to store tasks and to handle creation, retrieval, and deletion of tasks.

public class TaskManager
{
   private static List<Task> _TaskList = new List<Task>();
   // Create a new instance of Task object
   // and start a new thread to run this task
   public static Result CreateNewTask(Request request){
      var task = New Task(){
                TaskID =  System.Guid.NewGuid().ToString();
                .....
                };
      _TaskList.add(task);
      Thread thread = new Thread(task.Execute);
      task.Status = Status.Processing
      thread.start;
   }
   // This is also useful for purging tasks
   public static void RemoveTask(string taskID)
   public static Task GetTaskByID(string taskID)
}

Up to this point, we have our ‘backend’ built. Now we are going to create a WebAPI method to accept the request from the client side and a method for client side to check a task’s status:

[HttpPost]
public Result ProcessRequest(Request request){
// Submit a request which will be created as a task
// Return the TaskID back to client
}

[HttpGet]
public Result CheckStatus(String taskID){
// Call this every few seconds to check result
}

In this post I shared an idea to avoid client side timeout when calling WebAPI. We chose an approach that enables client side to check the result actively. Although it makes the client side a bit more complex, it is a reliable way to handle WebAPI calls that run a long time and also provides much more flexibility to the client side.

How to use soapUI 5.0 to post JSON to REST Service

I wrote an article on the same topic but with version 4.5.1 soapUI back in 2013. To post JSON with an 4.5.1 version soapUI, you almost have to trick the application by explicitly typing in the media type ‘application/json’, and there are also a few settings that don’t really make sense. I’ve always forgotten the steps and had to go back to check my blog post.

Since then I have tried a couple of newer versions of soapUI for the same task, hoping it got more intuitive, but eventually I stuck with the 4.5.1 version because I didn’t see much improvement in those versions. Today I get my hands on the latest 5.0 version of the tool. Still free, great!
First, I crate a new SOAP project. The ‘Opens dialog to create REST Service’ option is gone. OK. It is already simpler from the first step.
1

Now my project is created, I right click the project and select New REST Service from URI.

2

In the popup window, I put in the service endpoint where I am going to post my JSON to.

3

BOOM! It creates everything for me. No need to provide a Service Name, Parameter Name, or Method Name, everything is extracted from the endpoint provided. This is a great UI design because if out of nowhere it asks the user to provide a Service Name, she will be confused – What is the Service Name for?

4

It also automatically opens up Request1 where you can see it by default selects GET method.

5

I changed the Method to POST. It selects the correct Media Type for JSON posting. Just type in your JSON body and click the green arrow on the left top corner to post.

6

I am impressed by how easy the process becomes and glad that the development team is putting efforts on improving user experience, even though it is already a well-functional application. User experience is really a big part of software. A good UX can really change your life!

Post JSON to REST web service with soapUI

Among several web service test tools I have used, including WCFStorm, VS WCF Test Client, and other proprietary test tools, soapUI is my favorite. In a recent project, with the help of soapUI, I was able to add custom fields into HTTP header, compose the authentication header field, and POST JSON data to a REST web service. This post aims to provide a quick guide on how to add REST web services to soapUI and post JSON to the services.

Create a new project.

9-17-2013 11-00-15 AM

Provide a name and select the option Opens dialog to create REST service. Click OK.

9-17-2013 11-01-25 AM

In the next screen, provide the web service’s URL and select the option Extract Resource and Method from specified Endpoint, so that SoapUI can analyze the URL and find out the parameters in it. In my example, Site ID and User ID. Click OK.

9-17-2013 11-03-17 AM

You can define the parameter names and default values on the next screen. Click OK.

9-17-2013 11-07-07 AM

Next you can setup your method, including HTTP method and parameters. We will select POST here to demonstrate how to post JSON.

Click OK to finish creating the method.

9-17-2013 11-08-49 AM

Now we can go ahead to expend our project and method, and select the request just created. On the right pane, our parameters and end point are displayed.

9-17-2013 11-10-19 AM

Next, modify the Media Type to using application/json. There is no such an option in the dropdown list so it has to be typed.

Then type up the JSON you want to post.

Click on the green arrow on the left upper corner of the request. soapUI will post the data to the endpoint.

9-17-2013 11-16-16 AM

Click on the Raw tab to view the raw data just posted!

9-17-2013 11-19-27 AM

As it is demonstrated, soapUI is a very flexible and effective tool which offers a free, quick, and simple solution to test web services.