1

I have REST API Service. Below is API Controller method.

public class ExcelUploadController : ApiController
{
    [HttpPost]
    public async Task<HttpResponseMessage> Upload(HttpRequestMessage request)
    {
        if (!request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        HttpPostedData postedData = await Request.Content.ParseMultipartAsync();
        ProcessFile postedFile = new ProcessFile(postedData);

        return new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new StringContent(postedFile.Process())
        };
    }
  }

It returns JSON data of uploaded excel.

And below is my client code that uses RESTSharp

int ReadWriteTimeout = -1;
RestClient restClient = new RestClient("https://restfulservice.domain.com");
RestRequest restRequest = new RestRequest("api/excelupload/Upload");

int readWriteTimeout = restRequest.ReadWriteTimeout > 0
       ? restRequest.ReadWriteTimeout
       : ReadWriteTimeout;

restRequest.ReadWriteTimeout = -1;
restClient.Timeout = -1;
restClient.ReadWriteTimeout = -1;
restRequest.Method = Method.POST;
restRequest.AddHeader("Content-Type", "multipart/form-data");
restRequest.AddFile("content", location);
restRequest.AddParameter("DetailLineNumber", "4");

var response = restClient.Execute<List<ActualsDataViewModel>>(restRequest);

I am expecting my client to wait until service completes processing. But client is not waiting and response object is blank means statuscode is 0. And response.Data is null.

If I run the same REST API service in the localhost and replace below code in Client then it works perfectly fine.

 RestClient restClient = new RestClient("http://localhost:5100");

Why if same service is running on server (IIS) then client is not waiting for the response?

Parag Patil
  • 93
  • 2
  • 15
  • Have you checked this with another client? possibly add a quick test function that can be called using curl or web-client, to make sure the server actually sends something back? I've had problems with other things (not REST) that would would work on the local test server, but not on our official server. – Wolf-Kun Jun 28 '19 at 23:23
  • Have you checked your Rest API with a Rest client to make sure your service is working ? – sayah imad Jun 29 '19 at 00:32
  • What is output of `postedFile.Process()`? Can it be translated to `List` ? – Chetan Jun 29 '19 at 01:22
  • Hi @Wolf-Kun, sayah imad and Chetan Yes I have tested the server response using postman and google's RPC and it is working fine. Json is correctly converting to List. – Parag Patil Jun 29 '19 at 02:39

1 Answers1

0

Posting answer to save the time of others. I got it working.

I have added below line before calling the REST API. This answer was suggested by @Hans Vonn at Unable to read data from the transport connection : An existing connection was forcibly closed by the remote host second answer.

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
Parag Patil
  • 93
  • 2
  • 15