在客户端

时间:2017-11-07 08:31:59

标签: c# asp.net json self-host-webapi

我创建了一个自托管的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进行序列化和反序列化。

任何帮助都会很好。 注意:虽然不确定,但我想之前我能够返回&#34;对象&#34;直接不使用HttpResponseMessage或Json。

0 个答案:

没有答案