英文:
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
方法无法编译,因为 SocketsHttpHandler
的 SendAsync
是受保护的。
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<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// do stuff
return await base.SendAsync(request, cancellationToken); // will call InnerHandler.SendAsync
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论