ASP.NET Web API Request Batching and Service Call aggregation

What is HTTP Batching?

  • Group multiple HTTP requests into a single HTTP call. It defines a way to represent a complete HTTP request (headers and all) as a section in a single HTTP POST body.
  • Batch Http API calls together to reduce the number of HTTP connections client has to make. HTTP connection that client makes results in a certain amount of overhead,
  • Batch requests allow grouping multiple operations, into a single HTTP request payload
  • Minimize  number of messages that are passed between the client and the server and reduce network traffic, provides a smoother, promotes less chattiness.
  • Avoid Redundant HTTP API calls.
  • ASP.NET Web API provide OOB support for http batching through HttpBatchHandler. To enable batching Web API provides custom message handlers DefaultHttpBatchHandler which you can register per-route to handle the batch requests.
  • The key thing of Http batching is the Content-Type:multipart/mixed; boundary=12345665431 which is basically informing the reciever that the content of the POST request is made up of multiple parts seperated by the boundary indicator, Boundary Indicator can be any unique identifier.

Registering HTTP batch endpoint

You can use MapHttpBatchRoute, which is an HttpRouteCollection extension method, to create a batch endpoint. For example, the following will create a batch endpoint at “api/batch” in the App_Start WebApiConfig.cs file

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpBatchRoute(
routeName: “WebApiBatch”,
routeTemplate: “api/batch”,
batchHandler: new DefaultHttpBatchHandler(GlobalConfiguration.DefaultServer));

config.Routes.MapHttpRoute(
name: “DefaultApi”,
routeTemplate: “api/{controller}/{id}”,
defaults: new { id = RouteParameter.Optional }
);
}
}

Now, on the client side you can use the angular angular-http-batcher module to make batch requests. This module  hooks into the $http service and creates batch requests to batchable endpoints. Therefore, once the module is included as a dependency in your application it will automatically batch all batchable http request.

Set up the angular http batcher dependency to your angular module which can be app, controller or a service, preferably common service.

var batchedServiceClientModule = angular.module(‘batchedServiceClientModule ‘, [‘jcs.angular-http-batch’]);
batchedServiceClientModule.config([
‘httpbatchServiceClientProvider’,
function (httpbatchServiceClientProvider) {
httpbatchServiceClientProvider.setAllowedBatchEndpoint(
https://localhost:8080’,
https://localhost:8080/api/batch’);
}
]);

batchedServiceClientModule.factory(batchedServiceClient, [
‘$scope’,
‘$http’,
function ($scope, $http) {
 $http.get(https://localhost:8080/books’).then(function (data) {
console.log(‘success Books – ‘ + data.data);
}, function (err) {
console.log(‘error Books – ‘ + err);
});

$http.get(https://localhost:8080/books/1′).then(function (data) {
console.log(‘success books/1 – ‘ + data.data);
}, function (err) {
console.log(‘error books/1 – ‘ + err);
});

$http.put(https://localhost:8080/books’, {
Name: ‘Harry Potter’,
Autor: ‘J K Rowling’
}).then(function (data) {
console.log(‘success Post Books – ‘ + data.data);
}, function (err) {
console.log(‘error Post Books – ‘ + angular.fromJson(err));
});
}]);

Register ‘batchedServiceClientModule; with your mail ng-App

Advertisements

One thought on “ASP.NET Web API Request Batching and Service Call aggregation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s