天天看點

【ASP.NET Web API教程】5.4 ASP.NET Web API批處理器Batching Handler for ASP.NET Web API 5.4 ASP.NET Web API批處理器

本文引自:http://bradwilson.typepad.com/blog/2012/06/batching-handler-for-web-api.html

Brad Wilson | June 20, 2012

作者:Brad Wilson | 日期:2012-6-20

While there is no batching standard built into the HTTP protocol, there is a standard for MIME encoding HTTP request and response messages ("application/http" with "msgtype=request" and "msgtype=response", respectively). ASP.NET Web API has built-in support for both MIME multipart as well as encoded request and response messages, so we have all the building blocks we need to make a simple batch request handler.

當批處理标準尚未進入HTTP協定時,就已經有了對HTTP請求和響應消息進行編碼的MIME标準(分别采用“msgtype=request”和“msgtype=response”的“application/http”)。ASP.NET Web API對MIME的multipart(多部分内容類型)、以及經過編碼請求和響應消息都有内建的支援,是以,我們擁有了制作簡單的請求批處理器的全部建構塊。

All we need to make this work is an endpoint which can accept a multipart batch (an invention of our own), which then parses the requests, runs them sequentially, and returns the responses back in a multipart batch response.

我們所要做的全部工作隻是一個端點(endpoint),它可以接收一個multipart batch(多部批,一個我們自己發明的内容類型),然後用它對請求進行解析,按順序執行請求,并以一個multipart batch響應的形式傳回一個響應。

Starting with a Web API project (built against the latest nightly build), I updated the Web API config to look like this:

從一個Web API項目(根據最新版建立的項目)開始,我修改了Web API的config,它看上去像這樣:

I've inserted the handler for "api/batch" as our endpoint for batching requests, using the new "route-specific endpoint handler" feature in Web API. Note that since its URL is "api/batch", I made sure to add it before the default API route.

我已經為“api/batch”插入了處理器,以此作為對請求進行批處理的端點,這種做法利用了Web API中的“路由專用的端點處理器”特性。注,由于它的URL是“api/batch”,必須把它添加在預設的API路由之前。

Using async & await in .NET 4.5 makes the implementation of BatchHandler fairly straight-forward. All we need is an in-memory HttpServer which uses our existing configuration, so that the batched requests hit the exact same endpoints as requests from the Internet:

利用.NET 4.5中的async和await可以很直接地構造BatchHandler實作。我們所需要的隻是一個放在記憶體中的HttpServer,它使用目前配置,以便當請求來自Internet時,需要分批的請求會找到完全相同的端點:

Now we have an endpoint that we can send multipart/batch requests to, which are assumed to be HTTP request objects (anything which isn't is going to yield a 400).

現在,我們擁有了一個端點,我們能夠把multipart/batch請求發送給它,假設這些請求都是HTTP請求對象(任何不是HTTP請求的對象都會産生一個400狀态碼)。

On the client side, we make a multipart request and push requests into the multipart batch, one at a time:

在用戶端,我們形成了一個multipart請求,并把請求推入multipart batch,每次壓入一個請求:

In a console application, we can log both the request and response with code like this:

在一個控制台應用程式中,我們可以用以下代碼對請求和響應時行日志:

When I run this console application, I see output similar to this:

當運作這個控制台應用程式時,會看到輸出類似于這樣:

As you can see, our batch was successfully run, and the results show what we'd expected (the two real API calls returned back 200 with their data, and the bogus request we threw in the middle returns back a 404).

正如我們所看到的,批處理成功地運作了,并且顯示了我們所期望的結果(兩個真正的API調用傳回了帶有其資料的200狀态碼,而在中間壓入的僞造請求傳回了404狀态碼)。