如何从 HttpRequestMessage.Properties 迁移到 HttpRequestMessage.Options

Sha*_*dow 11 c# .net-5

升级到 net5 后,我收到过时警告: [CS0618] 'HttpRequestMessage.Properties' is obsolete: 'Use Options instead.' 但是似乎没有迁移指南。

即有以下代码

httpRequestMessage.Properties.Add(Key, Value);
Run Code Online (Sandbox Code Playgroud)

具体应该如何迁移呢?是

httpRequestMessage.Options.Set(new HttpRequestOptionsKey<TValue>(Key), Value);
Run Code Online (Sandbox Code Playgroud)

正确的?

met*_*ddy 8

我也曾为同样的事情而苦苦挣扎,但我找到了解决方案。以下是如何使用新的 Options 属性。

而不是request.Properties.Add("foo", true);
写:request.Options.Set(new HttpRequestOptionsKey<bool>("foo"), true);

要从响应中读取密钥:
response.RequestMessage.Options.TryGetValue(new HttpRequestOptionsKey<bool>("foo"), out bool foo);

说实话,我不知道他们为什么要改变它。奇怪的是我们需要为每个属性创建一个新的 HttpRequestOptionsKey 对象。但是它就是这样啊。

  • 您可以在 Jimi 链接的问题中看到它与浏览器和 WebAssembly 相关。我只是不明白为什么他们不添加可以处理它的扩展方法。 (3认同)