locked
Cancel long running post request from ASP.NET Web API 2 RRS feed

  • Question

  • User1973759594 posted

    I am developing a service which allows the client to upload large files (several hundred MB) to Asp.NET Web API. Under some circumstances, however, I would like to forcibly cancel the upload. However, it seems that whatever I do, the client uploads the file completely.

    My Web.config looks as follows:

    <system.web>
        <httpRuntime targetFramework="4.5" maxRequestLength="256"/>
    </system.web>
    <system.webServer>
      <security>
        <requestFiltering>
          <!--Set maximal total streaming file length-->
          <requestLimits maxAllowedContentLength="600000000" ></requestLimits>
        </requestFiltering>
     </security>
    </system.webServer>

    In other words, I allow large requests (600000000 bytes) and use a smaller input buffer (256 KB). I have the following simple controller method:

    [HttpPut]
    [Route("test")]
    public HttpResponseMessage Test()
    {
      Thread.Sleep(1000);
      throw new Exception("Stop");
    }

    When I perform a PUT with a large file, what happens is that the clients uploads some data until the buffer runs full; then the upload pauses for a while. After 1s, the server sends 500 InternalServerError. After this, the client happily keeps on uploading the rest of the data!

    When I try to cancel the upload from a delegating message handler, I see basically the same behavior:

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
      CancellationToken cancellationToken)
    {
      await Task.Delay(1000);
      var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
      {
        Content = new StringContent("Error")
      };
      return response;
    }

    Is there any way I can tell the server not to accept any further data? Even rough measures like forcibly closing the TCP socket would be acceptable.

    Friday, August 4, 2017 6:24 AM

Answers

  • User1973759594 posted

    Never mind, I found out how to do it (at least for IIS hosting):

    var ctx = Request.Properties["MS_HttpContext"] as HttpContextBase;
    if (ctx != null)
    {
    ctx.Request.Abort();
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 7, 2017 8:25 AM

All replies

  • User1168443798 posted

    Hi Christ,

    It seems you want to abort the request from server side, if so, I think you could try CancellationToken, and a simple code like below:

            public string Get(int id)
            {
                //cancel from server side
                long result=0;
                var tokenSource = new CancellationTokenSource();
                var token = tokenSource.Token;
                var task=Task.Run(() =>
                {
                    long n = 0;
                    while (!token.IsCancellationRequested)
                    {
                        //write the upload file
                        Console.WriteLine(n);
                        n = n + 1;
                        if (n > 10)
                        {
                            result = n;
                            tokenSource.Cancel();
                        }
                    }
                }, cancellationToken);
                task.Wait();
                return result.ToString();    
              
            }
    

    Best Regards,

    Edward

    Monday, August 7, 2017 6:58 AM
  • User1973759594 posted

    Never mind, I found out how to do it (at least for IIS hosting):

    var ctx = Request.Properties["MS_HttpContext"] as HttpContextBase;
    if (ctx != null)
    {
    ctx.Request.Abort();
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 7, 2017 8:25 AM