HttpClient very slow with Windows Authentication
up vote
4
down vote
favorite
I have an internal corporate API that I am calling via an HttpClient from a dotnet core website. The API is hosted in an IIS website that has Windows Authentication turned on. Authentication in the API is performed via this scheme. However the web requests are incredibly slow because the challenge-response process performed by the HttpClient contains large gaps of around 0.4 seconds between the 401 response being received from the API server and the HttpClient sending a subsequent (successful) request with the requisite Authorization header.
The HttpClient is normally injected via an HttpClientFactory but for the sake of clarity here is the equivalent inline code (the problem exists however the HttpClient is instantiated):
using (HttpClient httpClient = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }, true))
{
httpClient.BaseAddress = new System.Uri( "https://localhost:60491/myapi/api/Internal/");
var response = await httpClient.PutAsync(uri, content);
await ValidateResponseAsync(response);
}
On the server the logs then show a 0.4 second gap between the 401 response to the initial request and the next request with the Authorization header:
15:56:09.80114 [INF]
HTTP "PUT" "/api/Internal/Documents/989898989" responded 401 in 1.4533 ms.
User null
15:56:10.17185 [INF] Request
starting HTTP/1.1 PUT
http://localhost:60491/myapi/api/Internal/Documents/989898989
application/json 750
This is not a network-related issue as the website is local. I have performed exactly the same request via a web browser and the difference between the 401 being received by the browser and the next request is .001 seconds. For the entire PUT request (incorporating both the 401 and 204 responses) from the browser it takes 0.05 seconds and from the HttpClient 0.4 seconds. The performance difference is entirely attributable to the gap in sending the request with the Authorization header.
I can't use the PreAuthenticate flag on the HttpClientHandler as this is Uri-specific and my requests contain IDs that will differ between each one.
Does anyone have any ideas why this would be occurring and, better still, how to get round it?
c# iis asp.net-core windows-authentication dotnet-httpclient
add a comment |
up vote
4
down vote
favorite
I have an internal corporate API that I am calling via an HttpClient from a dotnet core website. The API is hosted in an IIS website that has Windows Authentication turned on. Authentication in the API is performed via this scheme. However the web requests are incredibly slow because the challenge-response process performed by the HttpClient contains large gaps of around 0.4 seconds between the 401 response being received from the API server and the HttpClient sending a subsequent (successful) request with the requisite Authorization header.
The HttpClient is normally injected via an HttpClientFactory but for the sake of clarity here is the equivalent inline code (the problem exists however the HttpClient is instantiated):
using (HttpClient httpClient = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }, true))
{
httpClient.BaseAddress = new System.Uri( "https://localhost:60491/myapi/api/Internal/");
var response = await httpClient.PutAsync(uri, content);
await ValidateResponseAsync(response);
}
On the server the logs then show a 0.4 second gap between the 401 response to the initial request and the next request with the Authorization header:
15:56:09.80114 [INF]
HTTP "PUT" "/api/Internal/Documents/989898989" responded 401 in 1.4533 ms.
User null
15:56:10.17185 [INF] Request
starting HTTP/1.1 PUT
http://localhost:60491/myapi/api/Internal/Documents/989898989
application/json 750
This is not a network-related issue as the website is local. I have performed exactly the same request via a web browser and the difference between the 401 being received by the browser and the next request is .001 seconds. For the entire PUT request (incorporating both the 401 and 204 responses) from the browser it takes 0.05 seconds and from the HttpClient 0.4 seconds. The performance difference is entirely attributable to the gap in sending the request with the Authorization header.
I can't use the PreAuthenticate flag on the HttpClientHandler as this is Uri-specific and my requests contain IDs that will differ between each one.
Does anyone have any ideas why this would be occurring and, better still, how to get round it?
c# iis asp.net-core windows-authentication dotnet-httpclient
When you access via the browser, are you signed on the same way as the program that is using HttpClient (e.g. are you using the service account and not your personal login account)? There are a number of settings that might differ, e.g. proxy settings. Also, consider getting a certificate, as the TLS validation forlocalhost
might be failing and falling back. Either that or usehttp:
-- addinghttps
for a localhost connection doesn't actually improve your security footing.
– John Wu
Nov 22 at 20:43
The logins are the same in both cases. I have set UseProxy on the HttpClientHandler to false (no proxy is set up for the browser). The localhost website uses the ISS Express Development Certificate which is trusted. In any event this same behaviour occurs on our test servers too so it's not specific to my local environment,
– strickt01
Nov 23 at 10:01
1
You may have to set up WIreshark and watch the traffic. Put them side by side and look for differences. The browser could be performing some sort of caching or have some other trick to improve throughput.
– John Wu
Nov 23 at 10:08
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I have an internal corporate API that I am calling via an HttpClient from a dotnet core website. The API is hosted in an IIS website that has Windows Authentication turned on. Authentication in the API is performed via this scheme. However the web requests are incredibly slow because the challenge-response process performed by the HttpClient contains large gaps of around 0.4 seconds between the 401 response being received from the API server and the HttpClient sending a subsequent (successful) request with the requisite Authorization header.
The HttpClient is normally injected via an HttpClientFactory but for the sake of clarity here is the equivalent inline code (the problem exists however the HttpClient is instantiated):
using (HttpClient httpClient = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }, true))
{
httpClient.BaseAddress = new System.Uri( "https://localhost:60491/myapi/api/Internal/");
var response = await httpClient.PutAsync(uri, content);
await ValidateResponseAsync(response);
}
On the server the logs then show a 0.4 second gap between the 401 response to the initial request and the next request with the Authorization header:
15:56:09.80114 [INF]
HTTP "PUT" "/api/Internal/Documents/989898989" responded 401 in 1.4533 ms.
User null
15:56:10.17185 [INF] Request
starting HTTP/1.1 PUT
http://localhost:60491/myapi/api/Internal/Documents/989898989
application/json 750
This is not a network-related issue as the website is local. I have performed exactly the same request via a web browser and the difference between the 401 being received by the browser and the next request is .001 seconds. For the entire PUT request (incorporating both the 401 and 204 responses) from the browser it takes 0.05 seconds and from the HttpClient 0.4 seconds. The performance difference is entirely attributable to the gap in sending the request with the Authorization header.
I can't use the PreAuthenticate flag on the HttpClientHandler as this is Uri-specific and my requests contain IDs that will differ between each one.
Does anyone have any ideas why this would be occurring and, better still, how to get round it?
c# iis asp.net-core windows-authentication dotnet-httpclient
I have an internal corporate API that I am calling via an HttpClient from a dotnet core website. The API is hosted in an IIS website that has Windows Authentication turned on. Authentication in the API is performed via this scheme. However the web requests are incredibly slow because the challenge-response process performed by the HttpClient contains large gaps of around 0.4 seconds between the 401 response being received from the API server and the HttpClient sending a subsequent (successful) request with the requisite Authorization header.
The HttpClient is normally injected via an HttpClientFactory but for the sake of clarity here is the equivalent inline code (the problem exists however the HttpClient is instantiated):
using (HttpClient httpClient = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }, true))
{
httpClient.BaseAddress = new System.Uri( "https://localhost:60491/myapi/api/Internal/");
var response = await httpClient.PutAsync(uri, content);
await ValidateResponseAsync(response);
}
On the server the logs then show a 0.4 second gap between the 401 response to the initial request and the next request with the Authorization header:
15:56:09.80114 [INF]
HTTP "PUT" "/api/Internal/Documents/989898989" responded 401 in 1.4533 ms.
User null
15:56:10.17185 [INF] Request
starting HTTP/1.1 PUT
http://localhost:60491/myapi/api/Internal/Documents/989898989
application/json 750
This is not a network-related issue as the website is local. I have performed exactly the same request via a web browser and the difference between the 401 being received by the browser and the next request is .001 seconds. For the entire PUT request (incorporating both the 401 and 204 responses) from the browser it takes 0.05 seconds and from the HttpClient 0.4 seconds. The performance difference is entirely attributable to the gap in sending the request with the Authorization header.
I can't use the PreAuthenticate flag on the HttpClientHandler as this is Uri-specific and my requests contain IDs that will differ between each one.
Does anyone have any ideas why this would be occurring and, better still, how to get round it?
c# iis asp.net-core windows-authentication dotnet-httpclient
c# iis asp.net-core windows-authentication dotnet-httpclient
asked Nov 22 at 17:01
strickt01
1,9081418
1,9081418
When you access via the browser, are you signed on the same way as the program that is using HttpClient (e.g. are you using the service account and not your personal login account)? There are a number of settings that might differ, e.g. proxy settings. Also, consider getting a certificate, as the TLS validation forlocalhost
might be failing and falling back. Either that or usehttp:
-- addinghttps
for a localhost connection doesn't actually improve your security footing.
– John Wu
Nov 22 at 20:43
The logins are the same in both cases. I have set UseProxy on the HttpClientHandler to false (no proxy is set up for the browser). The localhost website uses the ISS Express Development Certificate which is trusted. In any event this same behaviour occurs on our test servers too so it's not specific to my local environment,
– strickt01
Nov 23 at 10:01
1
You may have to set up WIreshark and watch the traffic. Put them side by side and look for differences. The browser could be performing some sort of caching or have some other trick to improve throughput.
– John Wu
Nov 23 at 10:08
add a comment |
When you access via the browser, are you signed on the same way as the program that is using HttpClient (e.g. are you using the service account and not your personal login account)? There are a number of settings that might differ, e.g. proxy settings. Also, consider getting a certificate, as the TLS validation forlocalhost
might be failing and falling back. Either that or usehttp:
-- addinghttps
for a localhost connection doesn't actually improve your security footing.
– John Wu
Nov 22 at 20:43
The logins are the same in both cases. I have set UseProxy on the HttpClientHandler to false (no proxy is set up for the browser). The localhost website uses the ISS Express Development Certificate which is trusted. In any event this same behaviour occurs on our test servers too so it's not specific to my local environment,
– strickt01
Nov 23 at 10:01
1
You may have to set up WIreshark and watch the traffic. Put them side by side and look for differences. The browser could be performing some sort of caching or have some other trick to improve throughput.
– John Wu
Nov 23 at 10:08
When you access via the browser, are you signed on the same way as the program that is using HttpClient (e.g. are you using the service account and not your personal login account)? There are a number of settings that might differ, e.g. proxy settings. Also, consider getting a certificate, as the TLS validation for
localhost
might be failing and falling back. Either that or use http:
-- adding https
for a localhost connection doesn't actually improve your security footing.– John Wu
Nov 22 at 20:43
When you access via the browser, are you signed on the same way as the program that is using HttpClient (e.g. are you using the service account and not your personal login account)? There are a number of settings that might differ, e.g. proxy settings. Also, consider getting a certificate, as the TLS validation for
localhost
might be failing and falling back. Either that or use http:
-- adding https
for a localhost connection doesn't actually improve your security footing.– John Wu
Nov 22 at 20:43
The logins are the same in both cases. I have set UseProxy on the HttpClientHandler to false (no proxy is set up for the browser). The localhost website uses the ISS Express Development Certificate which is trusted. In any event this same behaviour occurs on our test servers too so it's not specific to my local environment,
– strickt01
Nov 23 at 10:01
The logins are the same in both cases. I have set UseProxy on the HttpClientHandler to false (no proxy is set up for the browser). The localhost website uses the ISS Express Development Certificate which is trusted. In any event this same behaviour occurs on our test servers too so it's not specific to my local environment,
– strickt01
Nov 23 at 10:01
1
1
You may have to set up WIreshark and watch the traffic. Put them side by side and look for differences. The browser could be performing some sort of caching or have some other trick to improve throughput.
– John Wu
Nov 23 at 10:08
You may have to set up WIreshark and watch the traffic. Put them side by side and look for differences. The browser could be performing some sort of caching or have some other trick to improve throughput.
– John Wu
Nov 23 at 10:08
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
By default HttpClientHandler
has the property UseProxy
enabled by default. I have run in to similar situations where it was trying to find a proxy using the system default setting, set to "auto configue proxy", adding significant delay.
When you initialize HttpClientHandler
set UseProxy
to false and it may make the problem go away.
Hi Scott, thanks I thought this had fixed it but then saw that a flag had been set to authorize any request in the development environment so no 401 was being sent back upon the initial request. When I removed this and the 401 was returned then the 0.4 second delay occurred again between receiving the 401 and issuing the successful request with the Authorization header.
– strickt01
Nov 23 at 9:23
add a comment |
up vote
0
down vote
Can't you just set a faster timeout?
https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.timeout?redirectedfrom=MSDN&view=netframework-4.7.2
I'm assuming somehow this gets encoded in the request as well as used internally in the client. I have read that .net Core httpClient is very slow in 2.0, much faster in 2.1, you could also check the proxy settings as this can be a problem in .Net Core httpClient.
It's not timing out though so I can't see how changing the Timeout property can help? The request is successful, just slow. I am using core 2.1 so it's not that either.
– strickt01
Nov 23 at 9:17
Can you see what's happening on the network with Telerik Fiddler or Wireshark?
– Jeff Davies
Nov 24 at 10:30
add a comment |
up vote
-1
down vote
You should not be creating a new HttpClient per request, which is what your using statement is doing.
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
HttpClient is intended to be instantiated once and reused throughout the life of an application.
As I mention in my question, that is not the actual code that is running in the website. The HttpClient is in fact injected via an IHttpClientFactory set up by the DI and thus is not instantiated each time. I have merely used that code snippet for clarity to show the properties set up against the HttpClientHandler. Whether it is instantiated each time as per the code block or via the factory results in the same performance issue anyway.
– strickt01
Nov 22 at 17:47
My hunch would be try using Post instead of a Put, I've had similar issue with using PUT. Hope this helps
– Utkarsh Bais
Nov 22 at 23:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435516%2fhttpclient-very-slow-with-windows-authentication%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
By default HttpClientHandler
has the property UseProxy
enabled by default. I have run in to similar situations where it was trying to find a proxy using the system default setting, set to "auto configue proxy", adding significant delay.
When you initialize HttpClientHandler
set UseProxy
to false and it may make the problem go away.
Hi Scott, thanks I thought this had fixed it but then saw that a flag had been set to authorize any request in the development environment so no 401 was being sent back upon the initial request. When I removed this and the 401 was returned then the 0.4 second delay occurred again between receiving the 401 and issuing the successful request with the Authorization header.
– strickt01
Nov 23 at 9:23
add a comment |
up vote
1
down vote
By default HttpClientHandler
has the property UseProxy
enabled by default. I have run in to similar situations where it was trying to find a proxy using the system default setting, set to "auto configue proxy", adding significant delay.
When you initialize HttpClientHandler
set UseProxy
to false and it may make the problem go away.
Hi Scott, thanks I thought this had fixed it but then saw that a flag had been set to authorize any request in the development environment so no 401 was being sent back upon the initial request. When I removed this and the 401 was returned then the 0.4 second delay occurred again between receiving the 401 and issuing the successful request with the Authorization header.
– strickt01
Nov 23 at 9:23
add a comment |
up vote
1
down vote
up vote
1
down vote
By default HttpClientHandler
has the property UseProxy
enabled by default. I have run in to similar situations where it was trying to find a proxy using the system default setting, set to "auto configue proxy", adding significant delay.
When you initialize HttpClientHandler
set UseProxy
to false and it may make the problem go away.
By default HttpClientHandler
has the property UseProxy
enabled by default. I have run in to similar situations where it was trying to find a proxy using the system default setting, set to "auto configue proxy", adding significant delay.
When you initialize HttpClientHandler
set UseProxy
to false and it may make the problem go away.
answered Nov 22 at 20:12
Scott Chamberlain
97.5k24177317
97.5k24177317
Hi Scott, thanks I thought this had fixed it but then saw that a flag had been set to authorize any request in the development environment so no 401 was being sent back upon the initial request. When I removed this and the 401 was returned then the 0.4 second delay occurred again between receiving the 401 and issuing the successful request with the Authorization header.
– strickt01
Nov 23 at 9:23
add a comment |
Hi Scott, thanks I thought this had fixed it but then saw that a flag had been set to authorize any request in the development environment so no 401 was being sent back upon the initial request. When I removed this and the 401 was returned then the 0.4 second delay occurred again between receiving the 401 and issuing the successful request with the Authorization header.
– strickt01
Nov 23 at 9:23
Hi Scott, thanks I thought this had fixed it but then saw that a flag had been set to authorize any request in the development environment so no 401 was being sent back upon the initial request. When I removed this and the 401 was returned then the 0.4 second delay occurred again between receiving the 401 and issuing the successful request with the Authorization header.
– strickt01
Nov 23 at 9:23
Hi Scott, thanks I thought this had fixed it but then saw that a flag had been set to authorize any request in the development environment so no 401 was being sent back upon the initial request. When I removed this and the 401 was returned then the 0.4 second delay occurred again between receiving the 401 and issuing the successful request with the Authorization header.
– strickt01
Nov 23 at 9:23
add a comment |
up vote
0
down vote
Can't you just set a faster timeout?
https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.timeout?redirectedfrom=MSDN&view=netframework-4.7.2
I'm assuming somehow this gets encoded in the request as well as used internally in the client. I have read that .net Core httpClient is very slow in 2.0, much faster in 2.1, you could also check the proxy settings as this can be a problem in .Net Core httpClient.
It's not timing out though so I can't see how changing the Timeout property can help? The request is successful, just slow. I am using core 2.1 so it's not that either.
– strickt01
Nov 23 at 9:17
Can you see what's happening on the network with Telerik Fiddler or Wireshark?
– Jeff Davies
Nov 24 at 10:30
add a comment |
up vote
0
down vote
Can't you just set a faster timeout?
https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.timeout?redirectedfrom=MSDN&view=netframework-4.7.2
I'm assuming somehow this gets encoded in the request as well as used internally in the client. I have read that .net Core httpClient is very slow in 2.0, much faster in 2.1, you could also check the proxy settings as this can be a problem in .Net Core httpClient.
It's not timing out though so I can't see how changing the Timeout property can help? The request is successful, just slow. I am using core 2.1 so it's not that either.
– strickt01
Nov 23 at 9:17
Can you see what's happening on the network with Telerik Fiddler or Wireshark?
– Jeff Davies
Nov 24 at 10:30
add a comment |
up vote
0
down vote
up vote
0
down vote
Can't you just set a faster timeout?
https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.timeout?redirectedfrom=MSDN&view=netframework-4.7.2
I'm assuming somehow this gets encoded in the request as well as used internally in the client. I have read that .net Core httpClient is very slow in 2.0, much faster in 2.1, you could also check the proxy settings as this can be a problem in .Net Core httpClient.
Can't you just set a faster timeout?
https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.timeout?redirectedfrom=MSDN&view=netframework-4.7.2
I'm assuming somehow this gets encoded in the request as well as used internally in the client. I have read that .net Core httpClient is very slow in 2.0, much faster in 2.1, you could also check the proxy settings as this can be a problem in .Net Core httpClient.
edited Nov 22 at 18:18
answered Nov 22 at 18:06
Jeff Davies
458411
458411
It's not timing out though so I can't see how changing the Timeout property can help? The request is successful, just slow. I am using core 2.1 so it's not that either.
– strickt01
Nov 23 at 9:17
Can you see what's happening on the network with Telerik Fiddler or Wireshark?
– Jeff Davies
Nov 24 at 10:30
add a comment |
It's not timing out though so I can't see how changing the Timeout property can help? The request is successful, just slow. I am using core 2.1 so it's not that either.
– strickt01
Nov 23 at 9:17
Can you see what's happening on the network with Telerik Fiddler or Wireshark?
– Jeff Davies
Nov 24 at 10:30
It's not timing out though so I can't see how changing the Timeout property can help? The request is successful, just slow. I am using core 2.1 so it's not that either.
– strickt01
Nov 23 at 9:17
It's not timing out though so I can't see how changing the Timeout property can help? The request is successful, just slow. I am using core 2.1 so it's not that either.
– strickt01
Nov 23 at 9:17
Can you see what's happening on the network with Telerik Fiddler or Wireshark?
– Jeff Davies
Nov 24 at 10:30
Can you see what's happening on the network with Telerik Fiddler or Wireshark?
– Jeff Davies
Nov 24 at 10:30
add a comment |
up vote
-1
down vote
You should not be creating a new HttpClient per request, which is what your using statement is doing.
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
HttpClient is intended to be instantiated once and reused throughout the life of an application.
As I mention in my question, that is not the actual code that is running in the website. The HttpClient is in fact injected via an IHttpClientFactory set up by the DI and thus is not instantiated each time. I have merely used that code snippet for clarity to show the properties set up against the HttpClientHandler. Whether it is instantiated each time as per the code block or via the factory results in the same performance issue anyway.
– strickt01
Nov 22 at 17:47
My hunch would be try using Post instead of a Put, I've had similar issue with using PUT. Hope this helps
– Utkarsh Bais
Nov 22 at 23:16
add a comment |
up vote
-1
down vote
You should not be creating a new HttpClient per request, which is what your using statement is doing.
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
HttpClient is intended to be instantiated once and reused throughout the life of an application.
As I mention in my question, that is not the actual code that is running in the website. The HttpClient is in fact injected via an IHttpClientFactory set up by the DI and thus is not instantiated each time. I have merely used that code snippet for clarity to show the properties set up against the HttpClientHandler. Whether it is instantiated each time as per the code block or via the factory results in the same performance issue anyway.
– strickt01
Nov 22 at 17:47
My hunch would be try using Post instead of a Put, I've had similar issue with using PUT. Hope this helps
– Utkarsh Bais
Nov 22 at 23:16
add a comment |
up vote
-1
down vote
up vote
-1
down vote
You should not be creating a new HttpClient per request, which is what your using statement is doing.
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
HttpClient is intended to be instantiated once and reused throughout the life of an application.
You should not be creating a new HttpClient per request, which is what your using statement is doing.
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
HttpClient is intended to be instantiated once and reused throughout the life of an application.
answered Nov 22 at 17:34
Deepak
242
242
As I mention in my question, that is not the actual code that is running in the website. The HttpClient is in fact injected via an IHttpClientFactory set up by the DI and thus is not instantiated each time. I have merely used that code snippet for clarity to show the properties set up against the HttpClientHandler. Whether it is instantiated each time as per the code block or via the factory results in the same performance issue anyway.
– strickt01
Nov 22 at 17:47
My hunch would be try using Post instead of a Put, I've had similar issue with using PUT. Hope this helps
– Utkarsh Bais
Nov 22 at 23:16
add a comment |
As I mention in my question, that is not the actual code that is running in the website. The HttpClient is in fact injected via an IHttpClientFactory set up by the DI and thus is not instantiated each time. I have merely used that code snippet for clarity to show the properties set up against the HttpClientHandler. Whether it is instantiated each time as per the code block or via the factory results in the same performance issue anyway.
– strickt01
Nov 22 at 17:47
My hunch would be try using Post instead of a Put, I've had similar issue with using PUT. Hope this helps
– Utkarsh Bais
Nov 22 at 23:16
As I mention in my question, that is not the actual code that is running in the website. The HttpClient is in fact injected via an IHttpClientFactory set up by the DI and thus is not instantiated each time. I have merely used that code snippet for clarity to show the properties set up against the HttpClientHandler. Whether it is instantiated each time as per the code block or via the factory results in the same performance issue anyway.
– strickt01
Nov 22 at 17:47
As I mention in my question, that is not the actual code that is running in the website. The HttpClient is in fact injected via an IHttpClientFactory set up by the DI and thus is not instantiated each time. I have merely used that code snippet for clarity to show the properties set up against the HttpClientHandler. Whether it is instantiated each time as per the code block or via the factory results in the same performance issue anyway.
– strickt01
Nov 22 at 17:47
My hunch would be try using Post instead of a Put, I've had similar issue with using PUT. Hope this helps
– Utkarsh Bais
Nov 22 at 23:16
My hunch would be try using Post instead of a Put, I've had similar issue with using PUT. Hope this helps
– Utkarsh Bais
Nov 22 at 23:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435516%2fhttpclient-very-slow-with-windows-authentication%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
When you access via the browser, are you signed on the same way as the program that is using HttpClient (e.g. are you using the service account and not your personal login account)? There are a number of settings that might differ, e.g. proxy settings. Also, consider getting a certificate, as the TLS validation for
localhost
might be failing and falling back. Either that or usehttp:
-- addinghttps
for a localhost connection doesn't actually improve your security footing.– John Wu
Nov 22 at 20:43
The logins are the same in both cases. I have set UseProxy on the HttpClientHandler to false (no proxy is set up for the browser). The localhost website uses the ISS Express Development Certificate which is trusted. In any event this same behaviour occurs on our test servers too so it's not specific to my local environment,
– strickt01
Nov 23 at 10:01
1
You may have to set up WIreshark and watch the traffic. Put them side by side and look for differences. The browser could be performing some sort of caching or have some other trick to improve throughput.
– John Wu
Nov 23 at 10:08