Reputation: 2670
I have a strange error, that I can't reproduce on my own computer.
Here is the complete code I'm using:
public async Task LoadHeaderAndFooter()
{
//ignore any SSL errors
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var baseAddress = new Uri(Request.Url.GetComponents(UriComponents.Scheme | UriComponents.Host, UriFormat.Unescaped));
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
{
var oCookies = Request.Cookies;
for (var j = 0; j < oCookies.Count; j++)
{
var oCookie = oCookies.Get(j);
if (oCookie == null) continue;
var oC = new Cookie
{
Domain = baseAddress.Host,
Name = oCookie.Name,
Path = oCookie.Path,
Secure = oCookie.Secure,
Value = oCookie.Value
};
cookieContainer.Add(oC);
}
Header.Text = await client.GetStringAsync("/minside/loyalityadmin/header");
Footer.Text = await client.GetStringAsync("/minside/loyalityadmin/footer");
}
}
What happens is that the request starts, then waits for the timeout (30 sek default), the httpclient throw's a "task canceled" exeption. THEN the actuall request fires on the server.
Now this code is run in an .ascx.cs file. While the /header is a MVC controll with a .cshtml view. Both run on the same server.
How can I get this to work?
Upvotes: 1
Views: 408
Reputation: 2670
After even more testing it seems like restsharp.org also have the same issues on some plattforms (everywhere but my development plattform).
Turns out the problem is the session object. On IIS it is not possible to read the session state in parallell:
Access to ASP.NET session state is exclusive per session, which means that if two different users make concurrent requests, access to each separate session is granted concurrently. However, if two concurrent requests are made for the same session (by using the same SessionID value), the first request gets exclusive access to the session information. The second request executes only after the first request is finished. (The second session can also get access if the exclusive lock on the information is freed because the first request exceeds the lock time-out.) If the EnableSessionState value in the @ Page directive is set to ReadOnly, a request for the read-only session information does not result in an exclusive lock on the session data. However, read-only requests for session data might still have to wait for a lock set by a read-write request for session data to clear.
from https://msdn.microsoft.com/en-us/library/ms178581.aspx
So the solution was to simply not pass the session id:
if (oCookie == null || oCookie.Name == "ASP.NET_SessionId") continue;
Witch works fine, since the secondary request did not need access to the session. Any solution that makes it possible to use the session in both request would be recieved with thanks.
Upvotes: 1