public override void ExecuteResult(ActionContext context) { var resp = context.HttpContext.Response; resp.ContentType = "application/octet-stream"; using (var mem = new MemoryStream()) { var bw = new BinaryWriter(mem); if (SerializeObject.Serialize(ref bw)) { byte[] data = mem.ToArray(); // byte[] data = new byte[mem.Length]; // mem.Position = 0; // mem.Read(data, 0, data.Length); resp.ContentLength = data.Length; resp.Body.WriteAsync(data, 0, data.Length); } else { bw.Dispose(); mem.Dispose(); throw new InvalidOperationException(@"Unable to serialize object !"); } bw.Dispose(); mem.Dispose(); } }