简体   繁体   中英

IIS stops responding to requests until restart after running this method

I was trying to call Handler from Handler retaining my Session using this code but after i call it through ajax IIS will not respond to any other request until restart and i cant figure out why.

Im limited to .net 2.0

I was trying to find google similar issues but i had not found anything helpfull, only some things to look for like closing response and reader after using it .. have i overlooked something else?

public void Method() {
    string url = string.Format("OtherHandler.ashx");

    ASCIIEncoding encoder = new ASCIIEncoding();
    string poststring = String.Format("field1={0}&field2={1}", "a", "b");
    byte[] data = encoder.GetBytes(poststring); // a json object, or xml, whatever...

    CookieContainer cookieContainer = new CookieContainer();
    HttpCookieCollection httpCookies = HttpContext.Current.Request.Cookies;
    for (int j = 0; j < httpCookies.Count; j++)
    {
        HttpCookie httpCookie = httpCookies.Get(j);
        Cookie myCookie = new Cookie();

        // Convert between the System.Net.Cookie to a System.Web.HttpCookie...
        myCookie.Domain = context.Request.Url.Host;
        myCookie.Expires = httpCookie.Expires;
        myCookie.Name = httpCookie.Name;
        myCookie.Path = httpCookie.Path;
        myCookie.Secure = httpCookie.Secure;
        myCookie.Value = httpCookie.Value;

        cookieContainer.Add(myCookie);
    }

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(context.Request.Url.GetLeftPart(UriPartial.Path).ToString().Replace("ThisHandler.ashx", url));
    req.Proxy = null;
    req.Method = "POST";
    req.ContentType = "text/plain";
    req.ContentLength = data.Length;
    req.CookieContainer = cookieContainer;

    string responseFromServer = "";


    WebResponse response = req.GetResponse();
    Stream dataStream = response.GetResponseStream();
    HttpStatusCode statusCode = ((HttpWebResponse)response).StatusCode;

    StreamReader reader = new StreamReader(dataStream);
    responseFromServer = reader.ReadToEnd();

    response.Close();
    reader.Close();
}

I would like to be able to call function in one Handler from another Handler using POST method, sending several variable parameters (name=value like what forms or ajax or GET url does) and get some response from it while using same Session i have access in first handlers context.Session in .net 2 C# code in synchronous way without worrying it will hangs everything else than that original request (until it is resolved).

By default, Asp.Net allows only one concurrent request per Session . So if you have one request running, and during that request you are trying to run another request in the same session, then you are effectively deadlocking yourself.

You can work around it by having both handlers declared to require only read access to the session. This can be done by having the handlers implement the IReadOnlySessionState instead of IRequiresSessionState . The possible downside is, of course, that the handlers will not have access to store/update the Session, but the upside is that more than one request can run in parallel.

You could also perhaps work around this by not calling the second handler from the first using a web request. Instead make sure that the code that the other handler uses to do its job can be called directly from the first handler. This would be a better option since you eliminate the overhead of opening a connection, sending the request and receiving the response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM