0

在 SoapCore 中,HttpContext替换为MemoryStream下面包含的代码片段,或者可以在GitHub上找到。

// `HttpContext.Request.Body` comes in as a `FileBufferingReadStream`
private async Task ProcessOperation(HttpContext httpContext, IServiceProvider serviceProvider)
{
    Message responseMessage;

    //Reload the body to ensure we have the full message
    var memoryStream = new MemoryStream((int)httpContext.Request.ContentLength.GetValueOrDefault(1024));
    await httpContext.Request.Body.CopyToAsync(memoryStream).ConfigureAwait(false);
    memoryStream.Seek(0, SeekOrigin.Begin);
    httpContext.Request.Body = memoryStream;

    // ... Removed for brevity
}

我不知道如何将 body 转换为 astring所以我可以使用 XPath 来获取一些后代:

using var reader = new StreamReader(httpContext.Request.Body);
// The `requestBody` is cutoff and appears to only be half of the request, which
// appears to be the issue
var requestBody = await reader.ReadToEndAsync();

// Throws on this line with `XmlException There are multiple root elements`, which
// based on the requestBody being clipped makes sense
var xDocument = XDocument.Parse(requestBody);
var query = $"//{prefix}:{bodyElement}";
return xDocument.XPathSelectElement(query, GetXmlNamespaceManager(prefix, uri));

谁能看到为什么这不起作用,或者使用 SoapCore 来获取原始请求主体string而不是反序列化为模型?

4

0 回答 0