在 Microsoft.Owin.Testing.TestServer 的 HttpClient 上更改超时没有效果

Posted

技术标签:

【中文标题】在 Microsoft.Owin.Testing.TestServer 的 HttpClient 上更改超时没有效果【英文标题】:Changing Timeout on HttpClient of Microsoft.Owin.Testing.TestServer has no effect 【发布时间】:2019-01-24 10:06:54 【问题描述】:

我正在使用 .net 461、Microsoft.AspNet.WebApi 5.2.3

var testServer = Microsoft.Owin.Testing.TestServer.Create<MyConfig>();

//this has no effect, and Timeout is still the default 100 seconds
testServer.HttpClient.Timeout = TimeSpan.FromSeconds(200); 

如果我这样做,那么设置就会生效:

var client = new HttpClient

   BaseAddress = new Uri("baseaddress")
;

client.Timeout = TimeSpan.FromSeconds(200); // this change has effect

为什么我不能在第一个 sn-p 上更改 TimeOut 属性?

【问题讨论】:

【参考方案1】:

https://docs.microsoft.com/en-us/previous-versions/aspnet/dn782655(v%3dvs.113)

HttpClient 返回一个新的 HttpClient,它包装了 Handler 并且是 能够向 OWIN 管道提交请求。

所以每次调用testServer.HttpClient 时,都会为您创建一个新的HttpClient 实例,这就是您的更改无效的原因。

【讨论】:

以上是关于在 Microsoft.Owin.Testing.TestServer 的 HttpClient 上更改超时没有效果的主要内容,如果未能解决你的问题,请参考以下文章