Timeout on multiple POSTs to web api hosted in mono xsp

165 Views Asked by At

I've been using XSP to host my ASP.Net MVC website for a while (.Net 4.5.1, combination of Razor and WebForms) quite successfully.

However, when I come to use the WebApi, I am experiencing a problem (only on XSP, not IIS) :- the request stream resulting from POSTing to the reused connection times out when reading.

If I set "Connection" : "close" in the headers, then it works fine, or if I set HTTP to be anything other that 1.1, it also works (however, I am making the requests from a browser, so do not have control over these header values)

To test this, I overrode the standard JSON formatting:

    public class JsonFormmatter : BufferedMediaTypeFormatter
{
    public JsonFormmatter()
    {
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
    }

    public override object ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
    {
        return Deserialize(readStream, type);
    }

    public override void WriteToStream(Type type, object value, Stream writeStream, HttpContent content)
    {
        Serialize(value, writeStream);
    }

    public static void Serialize(object value, Stream s)
    {
        using (StreamWriter writer = new StreamWriter(s))
        using (JsonTextWriter jsonWriter = new JsonTextWriter(writer))
        {
            JsonSerializer ser = new JsonSerializer();
            ser.Serialize(jsonWriter, value);
            jsonWriter.Flush();
        }
    }

    public static object Deserialize(Stream s, Type type)
    {
        // READING JSON FROM THIS STREAM TIMES OUT!
        using (StreamReader reader = new StreamReader(s, Encoding.Default, true, 100000, true))
        using (JsonTextReader jsonReader = new JsonTextReader(reader))
        {
            JsonSerializer ser = new JsonSerializer();
            return ser.Deserialize(jsonReader, type);
        }
    }

    public override bool CanReadType(Type type)
    {
        return true;
    }

    public override bool CanWriteType(Type type)
    {
        return true;

And then, in App_Stat/WebApiConfig.cs:

    while (config.Formatters.Count > 0)
    {
        config.Formatters.RemoveAt(0);
    }
    config.Formatters.Add(new JsonFormmatter());

Posting JSON to an action on my web api controller times out when deserializing from the request stream.

Anyone have any ideas? Much appreciated!

0

There are 0 best solutions below