ServiceResponse.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System;
  8. using System.Diagnostics;
  9. using System.Net;
  10. namespace Aliyun.OSS.Common.Communication
  11. {
  12. internal abstract class ServiceResponse : ServiceMessage, IDisposable
  13. {
  14. public abstract HttpStatusCode StatusCode { get; }
  15. public abstract Exception Failure { get; }
  16. public virtual bool IsSuccessful()
  17. {
  18. return (int)StatusCode / 100 == (int)HttpStatusCode.OK / 100;
  19. }
  20. /// <summary>
  21. /// Throws the exception from communication if the status code is not 2xx.
  22. /// </summary>
  23. public virtual void EnsureSuccessful()
  24. {
  25. if (!IsSuccessful())
  26. {
  27. // Disposing the content should help users: If users call EnsureSuccessStatusCode(), an exception is
  28. // thrown if the response status code is != 2xx. I.e. the behavior is similar to a failed request (e.g.
  29. // connection failure). Users don't expect to dispose the content in this case: If an exception is
  30. // thrown, the object is responsible fore cleaning up its state.
  31. if (Content != null)
  32. {
  33. Content.Dispose();
  34. }
  35. Debug.Assert(Failure != null);
  36. throw Failure;
  37. }
  38. }
  39. public void Dispose()
  40. {
  41. Dispose(true);
  42. GC.SuppressFinalize(this);
  43. }
  44. protected virtual void Dispose(bool disposing)
  45. {
  46. }
  47. }
  48. }