How to migrate from HttpRequestMessage.Properties to HttpRequestMessage.Options

Viewed 2545

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?

1 Answers

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.");    
    }
}
Related