Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何传递HttpInputStream的内容并将其发送到c#net中的另一个Rest Api_C#_.net_Rest_Http_Inputstream - Fatal编程技术网

如何传递HttpInputStream的内容并将其发送到c#net中的另一个Rest Api

如何传递HttpInputStream的内容并将其发送到c#net中的另一个Rest Api,c#,.net,rest,http,inputstream,C#,.net,Rest,Http,Inputstream,我有一个REST API Web服务,它充当将调用重定向到另一个REST API服务的中间件 例如,在本例中,我在网页上上载了一个文件。这个Web服务接收文件并将它们发送到另一个实际处理文件的API 是这样的: public async Task<IHttpActionResult> ExecuteFileUpload() { IHttpActionResult res; try { var httpRequest = HttpContext.

我有一个REST API Web服务,它充当将调用重定向到另一个REST API服务的中间件

例如,在本例中,我在网页上上载了一个文件。这个Web服务接收文件并将它们发送到另一个实际处理文件的API

是这样的:

public async Task<IHttpActionResult> ExecuteFileUpload()
{
    IHttpActionResult res;
    try
    {
        var httpRequest = HttpContext.Current.Request;
        var requestedFiles = new List<System.IO.Stream>();
        var url = "http://localhost:2288" + "/api/v1/templates/upload";
        if (httpRequest.Files.Count > 0)
        {
            HttpFileCollection files = httpRequest.Files;
            using (var content = new MultipartFormDataContent())
            {
                int index = 0;
                foreach (var file in httpRequest.Files)
                {
                    var postedFile = httpRequest.Files[index];
                    var fileName = postedFile.FileName;
                    var fileInMemory = postedFile.InputStream;
                    content.Add(new StreamContent(fileInMemory), "f" + index, fileName);
                    index++;
                }
                res = await ForwardPost(url, content);
            }
        }
        else
            res = BadRequest();
    }
    catch (Exception ex)
    {
        res = InternalServerError(ex);
    }
    return res;
}
protected async Task<IHttpActionResult> ForwardPost(string url, MultipartFormDataContent forwardContent)
{
    IHttpActionResult res;
    using (var client = CreateClient())
    {
        using (var response = await client.PostAsync(url, forwardContent))
        {
            if (response.StatusCode == HttpStatusCode.OK)
            {
                var content = await response.Content.ReadAsAsync<JToken>();
                res = Ok(content);
            }
            else
            {
                res = InternalServerError();
            }
        }

        return res;
    }
}
它抛出System.IO.IOException

在写入所有字节之前无法关闭流

为什么会这样?
是否有办法解决此问题?

在ForwardPost函数中使用将处理调用方法中的forwardedContent。此dispose将尝试从源方法的请求对象中处理“postedFile.InputStream”引用。这可能与httprequest对象有着密切的联系

using (var response = await client.PostAsync(url, forwardContent))

解决方案是将postedFile.InputStream复制到新的memorystream,以便可以单独处理

在将postedFile.InputStream添加到新的MultipartFormDataContent之前,请尝试将其复制到新的内存流中。我尝试过这样做。然而,这导致我发送的内容变成了0字节的文件。最后我做了一些类似的事情:将其读入字节数组,然后将其加载回内存流。然而,我真的被错误消息弄糊涂了。因为在任何情况下,我都不会试图关闭这条小溪。我希望我能顺流而下。这样,尤其是当我试图上传一个更大的文件时,它不会占用内存。是的,它会占用你系统上的一些内存。可能有一种方法可以更直接地处理请求对象,这样您就可以在完成第一个请求后处理第二个请求。而不是直接在ForwardPost方法中
using (var response = await client.PostAsync(url, forwardContent))