WebApi Formatter使用ReadAsAsync和WriteToStreamAsync - Json被截断,而

本文关键字:Json 使用 Formatter ReadAsAsync WriteToStreamAsync WebApi | 更新日期: 2023-09-27 18:13:38

我正在使用C#, WebAPI 2,并得到一个奇怪的行为。

我有调用控制器的服务,内容有时可以作为Json发送,有时可以作为XML机制发送,用于进行调用,如下所示:

using (var client = new HttpClient())
{
    var pricingControllerUrl = CreateEndpoint(apiUrl);
    using (var response = (int)request.Metadata.InputType >= 3 ?  client.PostAsJsonAsync(pricingControllerUrl, request) : client.PostAsXmlWithSerializerAsync(pricingControllerUrl, request))
    {
        if (response.Result.IsSuccessStatusCode)
        {
            var session = response.Result.Content.ReadAsAsync<Session>(new List<MediaTypeFormatter>() { new XmlMediaTypeFormatter { UseXmlSerializer = true }, new JsonMediaTypeFormatter() }).Result;
            return session;
        }
    }
}
public static class HttpExtensions
{
    public static Task<HttpResponseMessage> PostAsXmlWithSerializerAsync<T>(this HttpClient client, string requestUri, T value)
    {
        return client.PostAsync(new Uri(requestUri), value,
                      new XmlMediaTypeFormatter { UseXmlSerializer = true }
                      );
    }
}

接收端(控制器),

public async Task<IHttpActionResult> PostSession([FromBody] Session session)
    {
    //do the calculations
    return Content(HttpStatusCode.OK, sessionResponse, new ReducedSessionFormatter(), this.Request.Content.Headers.ContentType);
    }

响应必须通过在分派之前删除一些信息来减少,下面的formatter用于促进这一点:

public class ReducedSessionFormatter : MediaTypeFormatter
{
    public ReducedSessionFormatter()
    {
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/json"));
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml"));
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml"));
    }
    public ReducedSessionFormatter(MediaTypeFormatter formatter) : base(formatter)
    {
    }
    public override bool CanReadType(Type type)
    {
        return false;
    }
    public override bool CanWriteType(Type type)
    {
        return type.IsAssignableFrom(typeof (Session));
    }
    protected XDocument ReduceXml(XDocument doc)
    {
        //removing stuff from xml
        return doc;
    }
    protected JObject ReduceJson(JObject serializedJson)
    {
        //removing stuff from json
        return serializedJson;
    }
    public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
    {
        if (content.Headers.ContentType.MediaType.Contains("xml"))
        {
            var doc = SerializeToXmlDocument(type, value).ToXDocument();
            doc = ReduceXml(doc);
            var settings = new XmlWriterSettings {Encoding = new UTF8Encoding(false)};
            using (XmlWriter w = XmlWriter.Create(writeStream, settings))
            {
                doc.Save(w);
            }
        }
        else
        {
            var json = new JavaScriptSerializer().Serialize(value);
            var serializedJson = (JObject)JsonConvert.DeserializeObject(json);
            var serializedJsonString = ReduceJson(serializedJson).ToString(Newtonsoft.Json.Formatting.None);
            var writer = new StreamWriter(writeStream);
            writer.Write(serializedJsonString);
        }
        var tcs = new TaskCompletionSource<object>();
        tcs.SetResult(null);
        return tcs.Task;
    }
    public XmlDocument SerializeToXmlDocument(Type type, object value)
    {
        var serializer = new XmlSerializer(type);
        XmlDocument xmlDocument = null;
        using (var memoryStream = new MemoryStream())
        {
            serializer.Serialize(memoryStream, value);
            memoryStream.Position = 0;
            using (var xtr = XmlReader.Create(memoryStream, new XmlReaderSettings {IgnoreWhitespace = true}))
            {
                xmlDocument = new XmlDocument();
                xmlDocument.Load(xtr);
            }
        }
        return xmlDocument;
    }
}
public static class XmlExtensions
{
    public static XDocument ToXDocument(this XmlDocument xmlDocument)
    {
        using (var nodeReader = new XmlNodeReader(xmlDocument))
        {
            nodeReader.MoveToContent();
            return XDocument.Load(nodeReader);
        }
    }
}
public static class JsonExtensions
{
    public static bool IsNullOrEmpty(this JToken token)
    {
        return (token == null) ||
               (token.Type == JTokenType.Array && !token.HasValues) ||
               (token.Type == JTokenType.Object && !token.HasValues) ||
               (token.Type == JTokenType.String && token.ToString() == String.Empty) ||
               (token.Type == JTokenType.Null);
    }
}

当在格式化器中聚合时,xml和json都是有效的,如果数据不使用格式化器就返回,一切都有效

奇怪的东西:当我使用formatter并发送回Json时,它会被截断,独立于它的长度。即使是非常小的对象(小于10k长度)也会在相同的位置被相同的对象切断,但不同的对象会有不同的长度,并且只适用于json,适用于xml…

如果json没有被tostring,它也会失败,如:

            var writer = new StreamWriter(writeStream);
            writer.Write(ReduceJson(serializedJson));

我已经添加了最小的解决方案来显示问题

这是怎么回事?为什么使用格式化器截断Json而不是XML的响应内容?

WebApi Formatter使用ReadAsAsync和WriteToStreamAsync - Json被截断,而

Ok,我已经找到了问题所在,它是在

中使用Task .Result;

var session = response.Result.Content.ReadAsAsync<Session>(new List<MediaTypeFormatter>() { new XmlMediaTypeFormatter { UseXmlSerializer = true }, new JsonMediaTypeFormatter() }).Result;

(和其他一些没有在问题中列出的机制),应该是await编辑。

一旦将.Result s替换为await,并将链中的所有方法标记为async -我开始得到完整的响应