升级到 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)
正确的?
我也曾为同样的事情而苦苦挣扎,但我找到了解决方案。以下是如何使用新的 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 对象。但是它就是这样啊。
归档时间: |
|
查看次数: |
7665 次 |
最近记录: |