Skip to content

Commit

Permalink
Use HttpRequestMessage.Options instead of Properties on net5.0
Browse files Browse the repository at this point in the history
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
1 parent c090005 commit 1a4fc56
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
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
4 changes: 4 additions & 0 deletions src/NuGet.Core/NuGet.Protocol/HttpSource/HttpRetryHandler.cs
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

0 comments on commit 1a4fc56

Please sign in to comment.