Skip to content

v2.0.0

Latest
Compare
Choose a tag to compare
@matteocontrini matteocontrini released this 24 Jan 17:14
c0b8b90

Changes

  • Target framework updated to .NET 6.0 (b0eaebf)
  • Enabled nullable types (bf727f7)
  • Upgraded Flurl dependency (URL builder) to v4 (6098e03, d7d2a27)
  • New EnsureSuccessStatusCode method on the response (5f1c96b)
  • New StatusCode property on the response (4503c87)
  • IHttpClientFactory.GetProxiedClient() now has the request URI parameter too (06677e9)
  • New extensible payload serialization: introduced JsonPayload, XmlPayload, FormUrlEncodedPayload, PlainTextPayload and removed the ContentType property (924b708, 7548e3c)
  • Changed how responses can be read: removed ReadBody, DownloadFileName, ResponseEncoding properties from the request and added new Read* methods to the response
  • The default HttpClientFactory can now be customized without replacing it (bbb7cbb, 6a960d3)
  • The default constructor of HttpRequest was removed
  • The default value for the Timeout property on the request was changed from TimeSpan.Zero to System.Threading.Timeout.InfiniteTimeSpan (9fa8d4c)
  • Exceptions now expose the request, response and elapsed time as properties (c4023db)

Upgrading from v1.x

Response reading and deserialization changes

If you used the Body property on the response, you should now use one of these methods to obtain the response:

  • Task<Stream> ReadStream()
  • Task<string> ReadString()
  • Task<string> ReadString(Encoding encoding)
  • Task<T?> ReadJson<T>(JsonSerializerOptions? options = null)
  • Task<string> DownloadFile(string path)
  • Task<T?> ReadXml<T>(XmlReaderSettings? settings = null)
  • Task<byte[]> ReadBytes()

All these methods except for ReadStream take into account the request timeout.

The updated README covers these methods, their usage and their pitfalls in greater detail.

Request serialization changes

If you used the request serialization feature, you should remove the ContentType property from requests and instead set the Payload with an instance of any class implementing the IPayload interface. Pass your payload to the constructor of these classes.

The library provides implementations for the following payload types:

  • JsonPayload (based on System.Text.Json)
  • XmlPayload (now with UTF-8 encoding instead of UTF-16)
  • FormUrlEncodedPayload
  • PlainTextPayload

With more likely coming in the future.

The updated README contains instructions on how to extend this mechanism and create new payload types.

Custom HttpClientFactorys

If you implemented your own IHttpClientFactory and/or inherited from HttpClientFactory, you must change the signature of the GetProxiedClient method as follows:

public HttpClient GetProxiedClient(Uri requestUri, Uri proxyUri)

Also, the default factory now allows for some basic customizations which weren't possible before without swapping the factory implementation. The updated README covers these new options in detail, which might remove the need for replacing the factory implementation altogether.

Default Timeout

If you relied on the fact that the default value of Timeout on HttpRequest was TimeSpan.Zero, the new default is System.Threading.Timeout.InfiniteTimeSpan (as it should always have been).