简体   繁体   English

IIS 停止响应请求,直到运行此方法后重新启动

[英]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.我试图从 Handler 调用 Handler 保留我的 Session 使用此代码,但在我通过 ajax 调用它之后,IIS 在重新启动之前不会响应任何其他请求,我无法弄清楚原因。

Im limited to .net 2.0我仅限于 .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).我希望能够使用 POST 方法从另一个处理程序调用一个处理程序中的函数,发送几个变量参数(名称=值,如表单或 ajax 或 GET url 所做的那样)并在使用相同的会话时从中获得一些响应我可以访问在 .net 2 C# 代码中的第一个处理程序 context.Session 中以同步方式而不用担心它会挂起除原始请求之外的所有其他内容(直到它被解决)。

By default, Asp.Net allows only one concurrent request per Session .默认情况下, Asp.Net每个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 .这可以通过让处理程序实现IReadOnlySessionState而不是IRequiresSessionStateIRequiresSessionState 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.您也可以通过不使用 Web 请求从第一个处理程序调用第二个处理程序来解决此问题。 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.这将是一个更好的选择,因为您消除了打开连接、发送请求和接收响应的开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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