Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use HttpRequestMessage.Options instead of Properties on net5.0
This dotnet/runtime PR marked Properties as obsolete:
dotnet/runtime#39182

Since we use warnings as errors, we have to change our code.
  • Loading branch information
zivkan committed Aug 31, 2020
commit 1a4fc5630d4a19d3c45d9930a5d4e5601f22a07c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -33,11 +34,18 @@ internal static HttpRequestMessage Clone(this HttpRequestMessage request)
clone.Headers.TryAddWithoutValidation(header.Key, header.Value);
}

#if NET5_0
var clonedOptions = (IDictionary<string, object>)clone.Options;
foreach (var option in request.Options)
{
clonedOptions.Add(option.Key, option.Value);
}
#else
foreach (var property in request.Properties)
{
clone.Properties.Add(property);
}

#endif
return clone;
}

Expand Down Expand Up @@ -110,13 +118,22 @@ public static void SetConfiguration(this HttpRequestMessage request, HttpRequest
throw new ArgumentNullException(nameof(configuration));
}

#if NET5_0
request.Options.Set(new HttpRequestOptionsKey<HttpRequestMessageConfiguration>(NuGetConfigurationKey), configuration);
#else
request.Properties[NuGetConfigurationKey] = configuration;
#endif
}

private static T GetProperty<T>(this HttpRequestMessage request, string key)
{

#if NET5_0
if (request.Options.TryGetValue<T>(new HttpRequestOptionsKey<T>(key), out T result))
#else
object result;
if (request.Properties.TryGetValue(key, out result) && result is T)
#endif
{
return (T)result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ public async Task<HttpResponseMessage> SendAsync(
headerStopwatch = new Stopwatch();
stopwatches.Add(headerStopwatch);
}
#if NET5_0
requestMessage.Options.Set(new HttpRequestOptionsKey<List<Stopwatch>>(StopwatchPropertyName), stopwatches);
#else
requestMessage.Properties[StopwatchPropertyName] = stopwatches;
#endif
var requestUri = requestMessage.RequestUri;

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,18 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
if (response.StatusCode == HttpStatusCode.Unauthorized ||
(configuration.PromptOn403 && response.StatusCode == HttpStatusCode.Forbidden))
{
IList<Stopwatch> stopwatches = null;
List<Stopwatch> stopwatches = null;

#if NET5_0
if (request.Options.TryGetValue(
new HttpRequestOptionsKey<List<Stopwatch>>(HttpRetryHandler.StopwatchPropertyName),
out stopwatches))
{
#else
if (request.Properties.TryGetValue(HttpRetryHandler.StopwatchPropertyName, out var value))
{
stopwatches = value as IList<Stopwatch>;
stopwatches = value as List<Stopwatch>;
#endif
if (stopwatches != null)
{
foreach (var stopwatch in stopwatches)
Expand Down