“一些方式 Microsoft 正在使用 SocketsHttpHandler 的受保护方法,但我不能”

huangapple go评论76阅读模式
英文:

Somehow Microsoft is using protected method from SocketsHttpHandler but I can't

问题

I'm trying to create derived class from HttpMessageHandler exactly like it was done here.
我试图创建一个从 HttpMessageHandler 派生的类,就像这里所做的那样。

I stumbled right in the beginning, my code so far:
我在一开始就遇到了问题,目前我的代码如下:

This is an exact copy of Microsoft's implementation of HttpClientHandler but method _socketsHttpHandler.SendAsync doesn't compile because SendAsync of SocketsHttpHandler is protected.
这是 Microsoft 实现的 HttpClientHandler 的精确副本,但 _socketsHttpHandler.SendAsync 方法无法编译,因为 SocketsHttpHandlerSendAsync 是受保护的。

Why it works in MS's code but not in mine? Is it some sort of extension method? If so then why IntelliSense doesn't provide help to insert the right using?
为什么它在微软的代码中工作正常,但在我的代码中不行?这是一种扩展方法吗?如果是的话,为什么 IntelliSense 没有提供插入正确 using 语句的帮助?

英文:

I'm trying to create derived class from HttpMessageHandler exactly like it was done here.
I stumbled right in the beginning, my code so far:

public class InternetClientHandler : HttpMessageHandler
    {
        private readonly SocketsHttpHandler _socketsHttpHandler;

        public InternetClientHandler()
        {
            _socketsHttpHandler = new SocketsHttpHandler();
        }

        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            return _socketsHttpHandler.SendAsync(request, cancellationToken); //ERROR
        }
    }

This is an exact copy of Microsoft's implementation of HttpClientHandler but method _socketsHttpHandler.SendAsync doesn't compile because SendAsync of SocketsHttpHandler is protected. Why it works in MS's code but not in mine? Is it some sort of extension method? If so then why Intelli Sense doesn't provide help to insert the right using?

答案1

得分: 5

You can't access SocketsHttpHandler.SendAsync because it is marked as protected internal. This means you either need to be in the same assembly (ie Microsoft's code), or you need to inherit from it. And you can't inherit because it's marked as sealed.

Instead, you should inherit from DelegatingHandler, which is part of the same assembly and therefore able to call it. This was designed for exactly this scenario.

public class InternetClientHandler : DelegatingHandler
{
	public InternetClientHandler() : base(new SocketsHttpHandler())
	{
        // change InnerHandler properties here
	}

	protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
	{
        // do stuff
		return await base.SendAsync(request, cancellationToken); // will call InnerHandler.SendAsync
	}
}
英文:

You can't access SocketsHttpHandler.SendAsync because it is marked as protected internal. This means you either need to be in the same assembly (ie Microsoft's code), or you need to inherit from it. And you can't inherit because it's marked as sealed.

Instead, you should inherit from DelegatingHandler, which is part of the same assembly and therefore able to call it. This was designed for exactly this scenario.

public class InternetClientHandler : DelegatingHandler
{
	public InternetClientHandler() : base(new SocketsHttpHandler())
	{
        // change InnerHandler properties here
	}

	protected override async Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
	{
        // do stuff
		return await base.SendAsync(request, cancellationToken); // will call InnerHandler.SendAsync
	}
}

huangapple
  • 本文由 发表于 2023年5月22日 19:14:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定