44
55using System ;
66using System . Collections . Generic ;
7+ using System . ComponentModel ;
78using System . Diagnostics ;
89#if NET5_0_OR_GREATER
910using System . Diagnostics . CodeAnalysis ;
@@ -40,15 +41,13 @@ public class HttpClientRequestAdapter : IRequestAdapter, IDisposable
4041 private readonly bool createdClient ;
4142 private readonly ObservabilityOptions obsOptions ;
4243 private readonly ActivitySource activitySource ;
43- #if ! NET5_0_OR_GREATER
44- private readonly Version httpVersion ;
45- #endif
44+ private readonly Version ? httpVersion ;
4645#if NETSTANDARD2_1_OR_GREATER
4746 private static readonly Version defaultHttpVersion = HttpVersion . Version20 ;
4847#elif NETSTANDARD2_0 || NETFRAMEWORK
4948 private static readonly Version defaultHttpVersion = HttpVersion . Version11 ;
5049#endif
51- #if NET5_0_OR_GREATER
50+
5251 /// <summary>
5352 /// Initializes a new instance of the <see cref="HttpClientRequestAdapter"/> class.
5453 /// <param name="authenticationProvider">The authentication provider.</param>
@@ -57,8 +56,15 @@ public class HttpClientRequestAdapter : IRequestAdapter, IDisposable
5756 /// <param name="httpClient">The native HTTP client.</param>
5857 /// <param name="observabilityOptions">The observability options.</param>
5958 /// </summary>
60- public HttpClientRequestAdapter ( IAuthenticationProvider authenticationProvider , IParseNodeFactory ? parseNodeFactory = null , ISerializationWriterFactory ? serializationWriterFactory = null , HttpClient ? httpClient = null , ObservabilityOptions ? observabilityOptions = null )
61- #else
59+ [ EditorBrowsable ( EditorBrowsableState . Never ) ]
60+ public HttpClientRequestAdapter ( IAuthenticationProvider authenticationProvider , IParseNodeFactory ? parseNodeFactory , ISerializationWriterFactory ? serializationWriterFactory , HttpClient ? httpClient , ObservabilityOptions ? observabilityOptions )
61+ : this ( authenticationProvider , parseNodeFactory , serializationWriterFactory , httpClient , observabilityOptions , httpVersion : null )
62+ {
63+ // Constructor without HttpVersion for runtime backwards compatibility. Adding a new
64+ // optional parameter provides *compile-time* backwards compatibility but throws a
65+ // MissingMethodException at runtime.
66+ }
67+
6268 /// <summary>
6369 /// Initializes a new instance of the <see cref="HttpClientRequestAdapter"/> class.
6470 /// <param name="authenticationProvider">The authentication provider.</param>
@@ -69,7 +75,6 @@ public HttpClientRequestAdapter(IAuthenticationProvider authenticationProvider,
6975 /// <param name="httpVersion">The HTTP version.</param>
7076 /// </summary>
7177 public HttpClientRequestAdapter ( IAuthenticationProvider authenticationProvider , IParseNodeFactory ? parseNodeFactory = null , ISerializationWriterFactory ? serializationWriterFactory = null , HttpClient ? httpClient = null , ObservabilityOptions ? observabilityOptions = null , Version ? httpVersion = null )
72- #endif
7378 {
7479 authProvider = authenticationProvider ?? throw new ArgumentNullException ( nameof ( authenticationProvider ) ) ;
7580 createdClient = httpClient == null ;
@@ -79,7 +84,9 @@ public HttpClientRequestAdapter(IAuthenticationProvider authenticationProvider,
7984 sWriterFactory = serializationWriterFactory ?? SerializationWriterFactoryRegistry . DefaultInstance ;
8085 obsOptions = observabilityOptions ?? new ObservabilityOptions ( ) ;
8186 activitySource = ActivitySourceRegistry . DefaultInstance . GetOrCreateActivitySource ( obsOptions . TracerInstrumentationName ) ;
82- #if ! NET5_0_OR_GREATER
87+ #if NET5_0_OR_GREATER
88+ this . httpVersion = httpVersion ;
89+ #else
8390 this . httpVersion = httpVersion ?? defaultHttpVersion ;
8491#endif
8592 }
@@ -620,7 +627,7 @@ private HttpRequestMessage GetRequestMessageFromRequestInformation(RequestInform
620627 Method = new HttpMethod ( requestInfo . HttpMethod . ToString ( ) . ToUpperInvariant ( ) ) ,
621628 RequestUri = requestUri ,
622629#if NET5_0_OR_GREATER
623- Version = client . DefaultRequestVersion ,
630+ Version = httpVersion ?? client . DefaultRequestVersion ,
624631 VersionPolicy = client . DefaultVersionPolicy
625632#else
626633 Version = httpVersion
0 commit comments