Http Batch Processing در Asp.Net Web Api
نویسنده: مسعود پاکدل
تاریخ: ۱۳۹۳/۱۱/۱۶ ۲۳:۴۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
var config = new HttpConfiguration();
config.Routes.MapHttpBatchRoute(
routeName: "Batch",
routeTemplate: "api/$batch",
batchHandler: new DefaultHttpBatchHandler(GlobalConfiguration.DefaultServer));
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "Default",
routeTemplate: "{controller}/{action}/{name}",
defaults: new { name = RouteParameter.Optional }
);
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.EnsureInitialized();
appBuilder.UseWebApi(config);
}
} var config = new HttpConfiguration(); HttpServer server = new HttpServer(config);
using System.Net.Http;
using System.Net.Http.Formatting;
public class Program
{
private static void Main(string[] args)
{
string baseAddress = "http://localhost:8080";
var client = new HttpClient();
var batchRequest = new HttpRequestMessage(HttpMethod.Post, baseAddress + "/api/$batch")
{
Content = new MultipartContent("mixed")
{
new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, baseAddress + "/api/Book/Add")
{
Content = new ObjectContent<string>("myBook", new JsonMediaTypeFormatter())
}),
new HttpMessageContent(new HttpRequestMessage(HttpMethod.Get, baseAddress + "/api/Book/GetAll"))
}
};
var batchResponse = client.SendAsync(batchRequest).Result;
MultipartStreamProvider streamProvider = batchResponse.Content.ReadAsMultipartAsync().Result;
foreach (var content in streamProvider.Contents)
{
var response = content.ReadAsHttpResponseMessageAsync().Result;
}
}
} config.Routes.MapHttpBatchRoute(
routeName: "WebApiBatch",
routeTemplate: "api/$batch",
batchHandler: new DefaultHttpBatchHandler(GlobalConfiguration.DefaultServer)
{
ExecutionOrder = BatchExecutionOrder.NonSequential
});