Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LogsController : ApiController
- {
- public HttpResponseMessage PostLog(List<LogDto> logs)
- {
- if (logs != null && logs.Any())
- {
- var goodLogs = new List<Log>();
- var badLogs = new List<LogBad>();
- foreach (var logDto in logs)
- {
- if (logDto.IsValid())
- {
- goodLogs.Add(logDto.ToLog());
- }
- else
- {
- badLogs.Add(logDto.ToLogBad());
- }
- }
- if (goodLogs.Any())
- {
- _logsRepo.Save(goodLogs);
- }
- if(badLogs.Any())
- {
- _logsBadRepo.Save(badLogs);
- }
- }
- return new HttpResponseMessage(HttpStatusCode.OK);
- }
- }
- public class gZipHandler : DelegatingHandler
- {
- protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
- {
- string encodingType = request.Headers.AcceptEncoding.First().Value;
- request.Content = new DeCompressedContent(request.Content, encodingType);
- return base.SendAsync(request, cancellationToken);
- }
- }
- public class DeCompressedContent : HttpContent
- {
- private HttpContent originalContent;
- private string encodingType;
- public DeCompressedContent(HttpContent content, string encodType)
- {
- originalContent = content;
- encodingType = encodType;
- }
- protected override bool TryComputeLength(out long length)
- {
- length = -1;
- return false;
- }
- protected override Task<Stream> CreateContentReadStreamAsync()
- {
- return base.CreateContentReadStreamAsync();
- }
- protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
- {
- Stream compressedStream = null;
- if (encodingType == "gzip")
- {
- compressedStream = new GZipStream(stream, CompressionMode.Decompress, leaveOpen: true);
- }
- return originalContent.CopyToAsync(compressedStream).ContinueWith(tsk =>
- {
- if (compressedStream != null)
- {
- compressedStream.Dispose();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement