Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate from HttpRequestMessage.Properties to HttpRequestMessage.Options

Tags:

c#

.net-5

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?

like image 519
Shadow Avatar asked Jan 01 '26 13:01

Shadow


1 Answers

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.

like image 140
metabuddy Avatar answered Jan 03 '26 03:01

metabuddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!