9

After googling for couple of days, I really cannot solve described issue. Hope here will find a solution

I'm using attached code when calling WCF service on the same server. I get Timeout error randomly in call WebReq.GetRequestStream()

When I'm check netstat I see that connection remains open, so probably is there a problem, but I don't know how to solve it

       //request inicialization
        HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
        WebReq.Method = "POST";
        WebReq.ContentType = "application/json; charset=utf-8";
        WebReq.ContentLength = buffer.Length;

        WebReq.Proxy = null;
        WebReq.KeepAlive = false; //also tried with true
        WebReq.AllowWriteStreamBuffering = false; //also tried with true

        //this produces an error
        using (Stream PostData = WebReq.GetRequestStream())
        {
            PostData.Write(buffer, 0, buffer.Length);
            PostData.Close();
        }

         //open and read response
         HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
         Stream Answer = WebResp.GetResponseStream();
         StreamReader _Answer = new StreamReader(Answer);

         WebResp.Close();

         //return string
         return _Answer.ReadToEnd();

Timeout is thrown mostly after some 10 seconds of idle time, but also after five or so requests in the row. Really cannot find a pattern.

What could be wrong with this code? Is there any other (better) way for calling WCF service?

AnzeR
  • 590
  • 4
  • 11
  • 23

4 Answers4

14

I don't know that it's definitely responsible for the problem, but you're only closing the web response if it doesn't throw an exception, and you're never closing the response stream. Use using statements:

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    return reader.ReadToEnd();
}

This could well explain the problem, as if you leave a response open it will keep the connection to the web server open - which means connection pooling then can't use that connection.

Eregrith
  • 4,263
  • 18
  • 39
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • It's a bit better, but I'm still getting timeouts. So, it's really strange. So after 1st link click is Loading and Loading and if in that time click on the same or any other link on the page then page loads in the second or so. So there is something wrong with Connection creation. As I suppose – AnzeR Feb 15 '10 at 08:39
  • I hadn't spotted that it was on the same machine... I wonder whether you're running out of thread pool threads - are these within the same process? – Jon Skeet Feb 15 '10 at 10:21
  • I'm quite new in .net programming. Have been programming in PHP for some years now, and there never notice such a problems. There are this things much much easier. So I 'm not sure what did you mean with "out of thread pool threads"? Withing one request I create two or so this HTTP request to WCF service, so it's withing same process/request. Should I use caching for this requests? – AnzeR Feb 16 '10 at 07:31
  • @AnzeR: I believe WCF will serve requests from the thread-pool; if you're calling into a service from itself, you could end up deadlocking if there are no more thread-pool threads free. You haven't really explained what the calling code is - is it within the same service, or is it a separate application? – Jon Skeet Feb 16 '10 at 07:39
  • Hm, I'm really not familiar with all this IIS settings. WEB and WCF resist withing same Application Pool, they are in the same Site, but set as different Application – AnzeR Feb 16 '10 at 08:59
  • @AnzeR: I would put some more tracing into your code - find out exactly where it's pausing. If you're currently just seeing it because the "outer" call is timing out, you don't really have enough information to analyze it yet. – Jon Skeet Feb 16 '10 at 09:27
  • If I run application in debug mode it stops on Stream PostData = WebReq.GetRequestStream() and after minute or two throw Timeout exception. This should be result of some previous request, but really don't know which one. – AnzeR Feb 16 '10 at 10:17
  • @AnzeR: GetRequestStream... okay, so again I suspect it's failing to connect. This may be due to some "maximum concurrent connection" limit within IIS which I'm not familiar with :( – Jon Skeet Feb 16 '10 at 10:43
  • I'm not sure that it's connection problem. Because it's not dependent to number of established TCP connection. I pasted debug log http://pastebin.com/m1aebd069, so there should be something useful – AnzeR Feb 16 '10 at 11:09
  • We are still getting timeouts. Probably we have to tall methods in WCF directly (it's possible because they are hosted together) and not through HTTP POST requests. It should also work much faster. \n It also strange that calls to this (the same one) services via Jquery Ajax works really smooth, and there is no problems. – AnzeR Feb 18 '10 at 07:55
6

I had this same issue, adding a call to HttpWebRequest.Abort() seemed to fix it.

Chris Fulstow
  • 41,170
  • 10
  • 86
  • 110
  • Thank you! Helped me to fix my issue. Remember: Always close your connections properly ;) – pila Dec 24 '12 at 14:37
0

Because this is really strange behaviour I would like to know if there are any other ways to call WCF service, hosted on same IIS server. I also thing, that creating TCP connection for that kind of calls in not really optimized and all other approaches should be much faster.

AnzeR
  • 590
  • 4
  • 11
  • 23
0

The first thing to keep in mind is to review the URI, parameters and headers being sent, specifically:

  • Reserved characters. Send reserved characters by the URI can bring problems ! * ' ( ) ; : @ & = + $ , / ? # []
  • URI Length: You should not exceed 2000 characters
  • Length headers: Most web servers do limit size of headers they accept. For example in Apache default limit is 8KB.

Keep in mind that if you want to send data from a longer length is recommended to send in the body of the message.

J.C. Gras
  • 4,934
  • 1
  • 37
  • 44