0

I have created a self hosted WEB API, which works pretty well. While returning an object through HttpResponseMessage, I get an exception in the client code. Exception reads - "The underlying connection was closed: An unexpected error occurred on a receive." and the Message is "An error occurred while sending the request.". Kindly see attachment.

Client Code:

internal async Task<object> AddBuiltInAsync(string featureType, Dictionary<string, string> specAttr)
{
    int rc = -1;
    object returnedFeatureId = null;
    try
    {
        FormUrlEncodedContent formContent = new FormUrlEncodedContent(specAttr);
        string jsonSpecAttr = JsonConvert.SerializeObject(specAttr);
        HttpResponseMessage response = await Client.PostAsync($"DWGImplODA/AddBuiltIn?featureType={featureType}&jsonSpecAttr={jsonSpecAttr}", formContent).ConfigureAwait(false);

        if (response.IsSuccessStatusCode)
        {
            object returnedFeatureId = await response.Content.ReadAsAsync<object>().ConfigureAwait(false);
            // returnedFeatureId = JsonConvert.DeserializeObject<object>(jsonReturnedFeatureId);
        }
        else
        {
            _logger.Info("AddBuiltInAsync: Failed");
        }
    }
    catch (Exception ex)
    {
        _logger.Error("AddBuiltInAsync: Exception - " + ex);
    }
    return returnedFeatureId;
}

Server Code:

[HttpPost]
public IHttpActionResult AddBuiltIn(string featureType, string jsonSpecAttr)
{
    HttpResponseMessage response = null;
    object returnedFeatureId = null;
    string jsonReturnedFeatureId = string.Empty;
    try
    {
        Dictionary<string, string> specAttr = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonSpecAttr);
        integrationImpl = (IIntegrationImpl)cacheObject.Get("integrationImpl");
        integrationImpl.AddBuiltIn(featureType, specAttr, out returnedFeatureId);
        //jsonReturnedFeatureId = JsonConvert.SerializeObject(returnedFeatureId);
    }
    catch (System.Exception ex)
    {
        Console.WriteLine("AddBuiltIn Exception - ", ex.StackTrace);
    }
    return Ok(returnedFeatureId);
}

As an alternative I have also tried returning a json string. However, controller fails to serialize the object(of a complex type Teigha.Runtime.RxClass) and returns nothing to the client. I am using json 6.0.0.0 for serialization and deserialization.

Any help would be nice. Note: Not sure though, but I think previously I was able to return "Object" directly without using HttpResponseMessage or Json.

Saadi
  • 2,211
  • 4
  • 21
  • 50
GeeKay
  • 1
  • 3

0 Answers0