Reputation: 2478
After upgrading to net5 I'm getting obsoletion warning:
[CS0618] 'HttpRequestMessage.Properties' is obsolete: 'Use Options instead.'
however there seems to be no migration guide.
I.e. there's following code
httpRequestMessage.Properties.Add(Key, Value);
How exactly it should be migrated? Is
httpRequestMessage.Options.Set(new HttpRequestOptionsKey<TValue>(Key), Value);
correct?
Upvotes: 14
Views: 10688
Reputation: 19
maybe this could give some idea how to get the value from HttpRequestMessage.Options
request.Options.TryGetValue(new HttpRequestOptionsKey<DefaultHttpContext>("HttpContext"), out var HttpContextValue);
Upvotes: 0
Reputation: 926
I struggled with the same thing and I found the solution. Here is how you should use the new Options property.
Instead of request.Properties.Add("foo", true);
Write: request.Options.Set(new HttpRequestOptionsKey<bool>("foo"), true);
To read the key from response:
response.RequestMessage.Options.TryGetValue(new HttpRequestOptionsKey<bool>("foo"), out bool foo);
To be honest, I don't know why they changed it. It's strange we need to create a new HttpRequestOptionsKey object for each property. But it is what it is.
Upvotes: 16
Reputation: 4939
You could switch on type - here's an example from attempting to clone an existing message:
foreach (var opt in httpRequestMessage.Options)
{
switch (opt.Value)
{
case string s:
clone.Options.Set<string>(new HttpRequestOptionsKey<string>(opt.Key), s);
break;
default:
throw new InvalidOperationException("Can't deal with non-string message options ... yet.");
}
}
Upvotes: -1