.NET Core 中 WebRequestHandler 的替代方案是什么? [英] What's the alternative to WebRequestHandler in .NET Core?

查看:37
本文介绍了.NET Core 中 WebRequestHandler 的替代方案是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 WebRequestHandler 在我的全栈 .NET 应用程序中设置 CachePolicyAuthenticationLevel.现在我正在将我的应用程序迁移到 .NET 核心,但找不到这些属性或 WebRequestHandler 的替代方案.有什么帮助吗?以下是我的用法:

I was using the WebRequestHandler for setting the CachePolicy and AuthenticationLevel in my full stack .NET application. Now I am migrating my application to .NET core and can't find an alternative to these properties or the WebRequestHandler. Any help? Following is my usage:

        var httpClientHandler = new WebRequestHandler
        {
            UseProxy = true,
            UseCookies = false,
            CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore),
            AuthenticationLevel = AuthenticationLevel.MutualAuthRequired
        };

推荐答案

CachePolicy:

.NET Core 中没有与 CachePolicy 等效的方法.但是,.NET Core 等效于 RequestCacheLevel.BypassCache.我在 this GitHub issue 中确认了这一点.

因此,虽然没有内置的 CachePolicy,但这种设计使您能够使用您喜欢的任何策略在 HttpClient 之上构建自己的缓存.

So while there is no built-in CachePolicy, this design enables you to build your own cache on top of HttpClient using any policy you like.

.NET Core 中的 WebRequest 提供了 AuthenticationLevel 属性,但如果您需要使用 HttpClient,这对您没有多大帮助.

WebRequest in .NET Core offers an AuthenticationLevel property, but that won't help you much if you need to use HttpClient.

您可以实现自定义 HttpMessageHandler 以传递到支持 AuthenticationLevel 的 HttpClient.为了更容易实现,您可以基于现有的 HttpMessageHandler 例如 Windows one.

You could implement a custom HttpMessageHandler to pass into HttpClient that supports AuthenticationLevel. To make it easier to implement, you could base it off an existing HttpMessageHandler such as the Windows one.

这篇关于.NET Core 中 WebRequestHandler 的替代方案是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆