0

我创建了一个自托管的 WEB API,效果很好。通过 HttpResponseMessage 返回对象时,客户端代码中出现异常。异常读取 - “底层连接已关闭:接收时发生意外错误。” 并且消息是“发送请求时发生错误。”。请看附件。

客户代码:

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;
}

服务器代码:

[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);
}

作为替代方案,我也尝试返回一个 json 字符串。但是,控制器无法序列化对象(复杂类型的 Teigha.Runtime.RxClass)并且不向客户端返回任何内容。我正在使用 json 6.0.0.0 进行序列化和反序列化。

你能帮忙的话,我会很高兴。注意:虽然不确定,但我认为以前我能够直接返回“对象”而不使用 HttpResponseMessage 或 Json。

4

0 回答 0