Created
April 1, 2020 10:31
-
-
Save ashoktandan/6678ed47eef5e74f1f54e89249e6b5a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Logging; | |
using MessagePack; | |
using RestSharp; | |
using System.Diagnostics; | |
namespace apitest.Controllers | |
{ | |
[ApiController] | |
[Route("[controller]")] | |
public class WeatherForecastController : ControllerBase | |
{ | |
private static readonly string[] Summaries = new[] | |
{ | |
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | |
}; | |
private readonly ILogger<WeatherForecastController> _logger; | |
public WeatherForecastController(ILogger<WeatherForecastController> logger) | |
{ | |
_logger = logger; | |
} | |
[HttpGet] | |
// [Produces("application/octet-stream")] | |
public IActionResult Get() | |
{ | |
HttpResponseMessage response = new HttpResponseMessage(); | |
var rng = new Random(); | |
var d = Enumerable.Range(1, 500).Select(index => new WeatherForecast | |
{ | |
Date = DateTime.Now.AddDays(index), | |
TemperatureC = rng.Next(-20, 55), | |
Summary = Summaries[rng.Next(Summaries.Length)] | |
}).ToList(); | |
var sd = MessagePackSerializer.Serialize(new test() { data = d }); | |
//response.Content = new ByteArrayContent(sd); | |
var ms = new MemoryStream(sd); | |
response.Content = new StreamContent(ms); | |
//var ds = MessagePackSerializer.Deserialize<WeatherForecast>(sd); | |
//response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment"); | |
//{FileName="data" }; | |
//response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); | |
return new FileContentResult(sd, "application/octet-stream"); | |
} | |
[MessagePackObject] | |
public class test { | |
[Key(1)] | |
public List<WeatherForecast> data { get; set; } | |
} | |
[HttpPost] | |
public void Post() { | |
Stopwatch sw = new Stopwatch(); | |
sw.Start(); | |
var client = new RestClient("https://localhost:5001/weatherforecast"); | |
var request = new RestRequest(Method.GET); | |
request.AddHeader("postman-token", "3e4f2699-414b-a5e6-701c-4d4f9689d458"); | |
request.AddHeader("cache-control", "no-cache"); | |
request.AddHeader("Content-Type", "application/octet-stream"); | |
request.AddHeader("accept", "application/octet-stream"); | |
IRestResponse response = client.Execute(request); | |
var d = MessagePackSerializer.Deserialize<test>(response.RawBytes); | |
sw.Stop(); | |
var we = sw.ElapsedMilliseconds; | |
// return response; | |
//var d = Enumerable.Range(1, 500).Select(index => new WeatherForecast | |
//{ | |
// Date = DateTime.Now.AddDays(index), | |
// TemperatureC = rng.Next(-20, 55), | |
// Summary = Summaries[rng.Next(Summaries.Length)] | |
//}).ToArray(); | |
//var d = new WeatherForecast | |
//{ | |
// Date = DateTime.Now.AddDays(1), | |
// TemperatureC = rng.Next(-20, 55), | |
// Summary = Summaries[rng.Next(Summaries.Length)] | |
//}; | |
//var sd = MessagePackSerializer.Serialize(d); | |
//response.Content = new ByteArrayContent(sd); | |
//var ds = MessagePackSerializer.Deserialize<WeatherForecast>(sd); | |
//response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); | |
//return response; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment