Capture the size of the response (in bytes) of a WebAPI method call












2














Is there a way to do this in WebAPI without reinventing the wheel? I can easily get the size of the object returned via WebAPI, but of course there are headers and serialization overhead. Since we are charged by bandwidth utilization, I'd like to know how big our responses are, but there does not seem to be an obvious mechanism for doing so.



EDIT
To be clear, I am looking for a way to do this programatically, so that I can report, analyze, and predict usage, and so I don't get caught by surprise with a bill.










share|improve this question





























    2














    Is there a way to do this in WebAPI without reinventing the wheel? I can easily get the size of the object returned via WebAPI, but of course there are headers and serialization overhead. Since we are charged by bandwidth utilization, I'd like to know how big our responses are, but there does not seem to be an obvious mechanism for doing so.



    EDIT
    To be clear, I am looking for a way to do this programatically, so that I can report, analyze, and predict usage, and so I don't get caught by surprise with a bill.










    share|improve this question



























      2












      2








      2


      2





      Is there a way to do this in WebAPI without reinventing the wheel? I can easily get the size of the object returned via WebAPI, but of course there are headers and serialization overhead. Since we are charged by bandwidth utilization, I'd like to know how big our responses are, but there does not seem to be an obvious mechanism for doing so.



      EDIT
      To be clear, I am looking for a way to do this programatically, so that I can report, analyze, and predict usage, and so I don't get caught by surprise with a bill.










      share|improve this question















      Is there a way to do this in WebAPI without reinventing the wheel? I can easily get the size of the object returned via WebAPI, but of course there are headers and serialization overhead. Since we are charged by bandwidth utilization, I'd like to know how big our responses are, but there does not seem to be an obvious mechanism for doing so.



      EDIT
      To be clear, I am looking for a way to do this programatically, so that I can report, analyze, and predict usage, and so I don't get caught by surprise with a bill.







      c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-web-api






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 21 '14 at 13:51

























      asked Feb 21 '14 at 12:48









      Jeremy Holovacs

      11.9k2175183




      11.9k2175183
























          2 Answers
          2






          active

          oldest

          votes


















          9














          Here is a message handler that can get the sizes you are looking for,



          public class ResponseSizeHandler : DelegatingHandler
          {
          protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
          {

          var response = await base.SendAsync(request, cancellationToken);
          if (response.Content != null)
          {
          await response.Content.LoadIntoBufferAsync();
          var bodylength = response.Content.Headers.ContentLength;
          var headerlength = response.Headers.ToString().Length;
          }
          return response;
          }
          }


          Just add an instance of this handler as the first message handler.



          config.MessageHandlers.Add(new ResponseSizeHandler());


          Don't be concerned by the LoadIntoBufferAsync, unless you actually do streaming content then almost all content is buffered by the host anyway, so doing it a little earlier in the pipeline will not add any extra overhead.






          share|improve this answer























          • Where Can i found config.MessageHandlers.Add in Web Api Core 2?
            – Drakoo
            Nov 21 at 10:17



















          2














          You can use Chrome F12 Developer Tools and its Network Tab or Fiddler tool to get the Content-Length of a particular HTTP Response.



          In Chrome (for example) -



          enter image description here



          In Fiddler (for example) -



          enter image description here



          UPDATE



          In Web API you can have a DelegatingHandler to record the response, and in DelegatingHandler you can have like this to get the ContentLength, in face you can get all the headers and response also -



          return base.SendAsync(request, cancellationToken).ContinueWith((task) =>
          {
          HttpResponseMessage response = task.Result;
          var contentLength = response.Content.Headers.ContentLength;
          return response;
          });





          share|improve this answer























          • Sorry, I wasn't clear. I want to capture this programmatically, from within the ASP.NET framework.
            – Jeremy Holovacs
            Feb 21 '14 at 13:45










          • @JeremyHolovacs, I updated my answer.
            – ramiramilu
            Feb 21 '14 at 14:17










          • In Web api core2 i dont have HttpRequestMessage, I have only HttpContext and HttpRequest.
            – Drakoo
            Nov 21 at 11:33











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21934933%2fcapture-the-size-of-the-response-in-bytes-of-a-webapi-method-call%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          9














          Here is a message handler that can get the sizes you are looking for,



          public class ResponseSizeHandler : DelegatingHandler
          {
          protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
          {

          var response = await base.SendAsync(request, cancellationToken);
          if (response.Content != null)
          {
          await response.Content.LoadIntoBufferAsync();
          var bodylength = response.Content.Headers.ContentLength;
          var headerlength = response.Headers.ToString().Length;
          }
          return response;
          }
          }


          Just add an instance of this handler as the first message handler.



          config.MessageHandlers.Add(new ResponseSizeHandler());


          Don't be concerned by the LoadIntoBufferAsync, unless you actually do streaming content then almost all content is buffered by the host anyway, so doing it a little earlier in the pipeline will not add any extra overhead.






          share|improve this answer























          • Where Can i found config.MessageHandlers.Add in Web Api Core 2?
            – Drakoo
            Nov 21 at 10:17
















          9














          Here is a message handler that can get the sizes you are looking for,



          public class ResponseSizeHandler : DelegatingHandler
          {
          protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
          {

          var response = await base.SendAsync(request, cancellationToken);
          if (response.Content != null)
          {
          await response.Content.LoadIntoBufferAsync();
          var bodylength = response.Content.Headers.ContentLength;
          var headerlength = response.Headers.ToString().Length;
          }
          return response;
          }
          }


          Just add an instance of this handler as the first message handler.



          config.MessageHandlers.Add(new ResponseSizeHandler());


          Don't be concerned by the LoadIntoBufferAsync, unless you actually do streaming content then almost all content is buffered by the host anyway, so doing it a little earlier in the pipeline will not add any extra overhead.






          share|improve this answer























          • Where Can i found config.MessageHandlers.Add in Web Api Core 2?
            – Drakoo
            Nov 21 at 10:17














          9












          9








          9






          Here is a message handler that can get the sizes you are looking for,



          public class ResponseSizeHandler : DelegatingHandler
          {
          protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
          {

          var response = await base.SendAsync(request, cancellationToken);
          if (response.Content != null)
          {
          await response.Content.LoadIntoBufferAsync();
          var bodylength = response.Content.Headers.ContentLength;
          var headerlength = response.Headers.ToString().Length;
          }
          return response;
          }
          }


          Just add an instance of this handler as the first message handler.



          config.MessageHandlers.Add(new ResponseSizeHandler());


          Don't be concerned by the LoadIntoBufferAsync, unless you actually do streaming content then almost all content is buffered by the host anyway, so doing it a little earlier in the pipeline will not add any extra overhead.






          share|improve this answer














          Here is a message handler that can get the sizes you are looking for,



          public class ResponseSizeHandler : DelegatingHandler
          {
          protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
          {

          var response = await base.SendAsync(request, cancellationToken);
          if (response.Content != null)
          {
          await response.Content.LoadIntoBufferAsync();
          var bodylength = response.Content.Headers.ContentLength;
          var headerlength = response.Headers.ToString().Length;
          }
          return response;
          }
          }


          Just add an instance of this handler as the first message handler.



          config.MessageHandlers.Add(new ResponseSizeHandler());


          Don't be concerned by the LoadIntoBufferAsync, unless you actually do streaming content then almost all content is buffered by the host anyway, so doing it a little earlier in the pipeline will not add any extra overhead.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 28 '15 at 17:41









          Yuval Itzchakov

          112k26168237




          112k26168237










          answered Feb 21 '14 at 14:16









          Darrel Miller

          111k27166223




          111k27166223












          • Where Can i found config.MessageHandlers.Add in Web Api Core 2?
            – Drakoo
            Nov 21 at 10:17


















          • Where Can i found config.MessageHandlers.Add in Web Api Core 2?
            – Drakoo
            Nov 21 at 10:17
















          Where Can i found config.MessageHandlers.Add in Web Api Core 2?
          – Drakoo
          Nov 21 at 10:17




          Where Can i found config.MessageHandlers.Add in Web Api Core 2?
          – Drakoo
          Nov 21 at 10:17













          2














          You can use Chrome F12 Developer Tools and its Network Tab or Fiddler tool to get the Content-Length of a particular HTTP Response.



          In Chrome (for example) -



          enter image description here



          In Fiddler (for example) -



          enter image description here



          UPDATE



          In Web API you can have a DelegatingHandler to record the response, and in DelegatingHandler you can have like this to get the ContentLength, in face you can get all the headers and response also -



          return base.SendAsync(request, cancellationToken).ContinueWith((task) =>
          {
          HttpResponseMessage response = task.Result;
          var contentLength = response.Content.Headers.ContentLength;
          return response;
          });





          share|improve this answer























          • Sorry, I wasn't clear. I want to capture this programmatically, from within the ASP.NET framework.
            – Jeremy Holovacs
            Feb 21 '14 at 13:45










          • @JeremyHolovacs, I updated my answer.
            – ramiramilu
            Feb 21 '14 at 14:17










          • In Web api core2 i dont have HttpRequestMessage, I have only HttpContext and HttpRequest.
            – Drakoo
            Nov 21 at 11:33
















          2














          You can use Chrome F12 Developer Tools and its Network Tab or Fiddler tool to get the Content-Length of a particular HTTP Response.



          In Chrome (for example) -



          enter image description here



          In Fiddler (for example) -



          enter image description here



          UPDATE



          In Web API you can have a DelegatingHandler to record the response, and in DelegatingHandler you can have like this to get the ContentLength, in face you can get all the headers and response also -



          return base.SendAsync(request, cancellationToken).ContinueWith((task) =>
          {
          HttpResponseMessage response = task.Result;
          var contentLength = response.Content.Headers.ContentLength;
          return response;
          });





          share|improve this answer























          • Sorry, I wasn't clear. I want to capture this programmatically, from within the ASP.NET framework.
            – Jeremy Holovacs
            Feb 21 '14 at 13:45










          • @JeremyHolovacs, I updated my answer.
            – ramiramilu
            Feb 21 '14 at 14:17










          • In Web api core2 i dont have HttpRequestMessage, I have only HttpContext and HttpRequest.
            – Drakoo
            Nov 21 at 11:33














          2












          2








          2






          You can use Chrome F12 Developer Tools and its Network Tab or Fiddler tool to get the Content-Length of a particular HTTP Response.



          In Chrome (for example) -



          enter image description here



          In Fiddler (for example) -



          enter image description here



          UPDATE



          In Web API you can have a DelegatingHandler to record the response, and in DelegatingHandler you can have like this to get the ContentLength, in face you can get all the headers and response also -



          return base.SendAsync(request, cancellationToken).ContinueWith((task) =>
          {
          HttpResponseMessage response = task.Result;
          var contentLength = response.Content.Headers.ContentLength;
          return response;
          });





          share|improve this answer














          You can use Chrome F12 Developer Tools and its Network Tab or Fiddler tool to get the Content-Length of a particular HTTP Response.



          In Chrome (for example) -



          enter image description here



          In Fiddler (for example) -



          enter image description here



          UPDATE



          In Web API you can have a DelegatingHandler to record the response, and in DelegatingHandler you can have like this to get the ContentLength, in face you can get all the headers and response also -



          return base.SendAsync(request, cancellationToken).ContinueWith((task) =>
          {
          HttpResponseMessage response = task.Result;
          var contentLength = response.Content.Headers.ContentLength;
          return response;
          });






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 21 '14 at 14:17

























          answered Feb 21 '14 at 13:11









          ramiramilu

          14.6k43959




          14.6k43959












          • Sorry, I wasn't clear. I want to capture this programmatically, from within the ASP.NET framework.
            – Jeremy Holovacs
            Feb 21 '14 at 13:45










          • @JeremyHolovacs, I updated my answer.
            – ramiramilu
            Feb 21 '14 at 14:17










          • In Web api core2 i dont have HttpRequestMessage, I have only HttpContext and HttpRequest.
            – Drakoo
            Nov 21 at 11:33


















          • Sorry, I wasn't clear. I want to capture this programmatically, from within the ASP.NET framework.
            – Jeremy Holovacs
            Feb 21 '14 at 13:45










          • @JeremyHolovacs, I updated my answer.
            – ramiramilu
            Feb 21 '14 at 14:17










          • In Web api core2 i dont have HttpRequestMessage, I have only HttpContext and HttpRequest.
            – Drakoo
            Nov 21 at 11:33
















          Sorry, I wasn't clear. I want to capture this programmatically, from within the ASP.NET framework.
          – Jeremy Holovacs
          Feb 21 '14 at 13:45




          Sorry, I wasn't clear. I want to capture this programmatically, from within the ASP.NET framework.
          – Jeremy Holovacs
          Feb 21 '14 at 13:45












          @JeremyHolovacs, I updated my answer.
          – ramiramilu
          Feb 21 '14 at 14:17




          @JeremyHolovacs, I updated my answer.
          – ramiramilu
          Feb 21 '14 at 14:17












          In Web api core2 i dont have HttpRequestMessage, I have only HttpContext and HttpRequest.
          – Drakoo
          Nov 21 at 11:33




          In Web api core2 i dont have HttpRequestMessage, I have only HttpContext and HttpRequest.
          – Drakoo
          Nov 21 at 11:33


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21934933%2fcapture-the-size-of-the-response-in-bytes-of-a-webapi-method-call%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python