Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request CachePolicy isn't applied in HTTP request header #31320

Closed
benZhai01 opened this issue Oct 28, 2019 · 12 comments · Fixed by #60913
Closed

Request CachePolicy isn't applied in HTTP request header #31320

benZhai01 opened this issue Oct 28, 2019 · 12 comments · Fixed by #60913
Labels
area-System.Net help wanted [up-for-grabs] Good issue for external contributors tenet-compatibility Incompatibility with previous versions or .NET Framework
Milestone

Comments

@benZhai01
Copy link

Steps:

  1. create a Console App(.net core 3.0)
  2. apply following code
static void Main(string[] args)
{
    var url = "https://docs.microsoft.com/en-us/dotnet/api/system.net.cache.requestcachelevel?view=netframework-4.8";
    var req = System.Net.HttpWebRequest.Create(url);
    req.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
    var res = req.GetResponse();
}
  1. run sample.

Actual Behavior

It's observerd that http request header doesn't include the cache setting.

image

Expected Behavior

Runing with the same code in Console App against .net framework, we can get following request header.
image

My Question

  1. Is it a bug or by design?
@benZhai01 benZhai01 changed the title Request CachePolicy doesn't applied in .net core 3.0 Request CachePolicy doesn't applied in HTTP request header in .net core 3.0 Oct 28, 2019
@wfurt
Copy link
Member

wfurt commented Oct 28, 2019

cc: @davidsh

@benZhai01
Copy link
Author

Any updates here? I think it's a very basic property.

@davidsh
Copy link
Contributor

davidsh commented Nov 4, 2019

Is it a bug or by design?

Unlike .NET Framework, .NET Core does not have any caching features for HTTP. It does not support .NET Framework related System.Net.Cache features.

There are really 2 parts to the caching logic.

  1. HTTP request headers that get sent out when HttpWebRequest.CachePolicy is set.
  2. HTTP response processing for either storing a response in the cache for future use, or fetching a cached response instead of having the request go to the server.

(1) can probably be fixed so that we have some compatibility with .NET Framework behavior, at least in terms of what request headers get added to the outgoing HTTP request.

(2) is not going to be fixed. There are no plans to add the System.Net.Cache feature to .NET Core.

@benZhai01
Copy link
Author

@davidsh Thanks for detail explanation.
I think (1) is enough on my side.
BTW, can this fixing be included in .netcore 3.1 release? It's really a annoying issue for me.

@davidsh
Copy link
Contributor

davidsh commented Nov 4, 2019

BTW, can this fixing be included in .netcore 3.1 release? It's really a annoying issue for me.

The change has to be made to the 'master' branch first which is targeting .NET 5. After that, the change can be considered for .NET Core 3.1.

We're getting close to .NET Core 3.1 RTM, so, it's possible it won't make that release. But it could be considered for a servicing release to .NET Core 3.1, i.e. .NET Core 3.1.1, 3.1.2, etc.

@benZhai01
Copy link
Author

@davidsh that's great, thank you.

@benZhai01
Copy link
Author

Workaround:

HttpWebRequest wr;
wr.Headers.Add("Cache-Control", "no-store,no-cache");
wr.Headers.Add("Pragma", "no-cache");

@dotMorten
Copy link

dotMorten commented Nov 17, 2019

There are no plans to add the System.Net.Cache feature to .NET Core.

Could you make it a plan? Currently I'm looking at telling our customers how awesome .NET Core is, but errrrr... you should also look forward to slower responses because repeated web requests are no longer relying on fast 304 responses. It's not a very good selling point for moving over from .NET Framework.

@dotMorten
Copy link

dotMorten commented Nov 17, 2019

Quick and dirty cache implementation showed a 33x perf increase on repeated requests when implementing cache-control max-age and/or etag:

Method Mean Error StdDev Median Ratio Gen 0 Gen 1 Gen 2 Allocated
UseSocketsHttpHandler 48.710 ms 0.4979 ms 0.4414 ms 48.694 ms 1.00 - - - 243.08 KB
UseHttpClientHandler 47.781 ms 0.5331 ms 0.4451 ms 47.884 ms 0.98 - - - 242.25 KB
UseCacheHttpHandler 1.252 ms 0.0665 ms 0.1907 ms 1.199 ms 0.03 - - - 247.48 KB

Test was done on an above-average high-speed internet connection. Effect would of course be A LOT bigger on slower networks.

@karelz
Copy link
Member

karelz commented Jan 7, 2020

Triage: Looks like low priority. HttpWebRequest is deprecated and we recommend everyone to use HttpClient instead. There is a workaround for people who really need it.

That said, the work is probably easy and we would take a PR for the part (1) from #31320 (comment)

@dotMorten
Copy link

Perhaps I was slightly misreading this thread. If it's specific to saying you want a fresh copy from the server, I get it should be sending that. I was conflating it with the settings that also tells the client to pull from the local cache as well if possible (which is also part of the CachePolicy property / RequestCacheLevel enumeration)

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the Future milestone Feb 1, 2020
@karelz karelz changed the title Request CachePolicy doesn't applied in HTTP request header in .net core 3.0 Request CachePolicy isn't applied in HTTP request header Feb 22, 2020
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Oct 27, 2021
@pedrobsaila
Copy link
Contributor

Hi.
I made a PR #60913 for this issue

@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Nov 9, 2021
@karelz karelz modified the milestones: Future, 7.0.0 Nov 23, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Dec 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net help wanted [up-for-grabs] Good issue for external contributors tenet-compatibility Incompatibility with previous versions or .NET Framework
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants