【问题标题】:ASP.NET CORE 2 System.Net.Sockets.SocketException: Whenever uploading filesASP.NET CORE 2 System.Net.Sockets.SocketException:每当上传文件时
【发布时间】:2019-06-20 08:57:05
【问题描述】:

当我尝试通过 IIS/服务器上传文件时,我总是收到以下错误消息,它在本地工作,但在服务器上,它总是有这个问题。

有人知道可能是什么问题吗?

错误信息:

调用事件处理程序失败: Microsoft.AspNetCore.Connections.ConnectionResetException:现有的 连接被远程主机强行关闭---> System.Net.Sockets.SocketException:现有连接是 被远程主机强行关闭 Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketAwaitable.GetResult() 在 Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketConnection.ProcessReceives() 在 Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketConnection.DoReceive() --- 内部异常堆栈跟踪结束 --- 在 System.IO.Pipelines.PipeCompletion.ThrowLatchedException() 在 System.IO.Pipelines.Pipe.GetReadResult(ReadResult& 结果) 在 System.IO.Pipelines.Pipe.GetReadAsyncResult() 在 System.IO.Pipelines.Pipe.DefaultPipeReader.GetResult(Int16 令牌)
在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1MessageBody.PumpAsync() 在 System.IO.Pipelines.PipeCompletion.ThrowLatchedException() 在 System.IO.Pipelines.Pipe.GetReadResult(ReadResult& 结果) 在 System.IO.Pipelines.Pipe.ReadAsync(CancellationToken 令牌)在 System.IO.Pipelines.Pipe.DefaultPipeReader.ReadAsync(CancellationToken 取消令牌)在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.ReadAsync(Memory1 buffer, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.ReadAsyncInternal(Memory1 缓冲区,CancellationToken 取消令牌)在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.Read(Byte[] 缓冲区,Int32 偏移量,Int32 计数)在 Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 计数)在 System.IO.StreamReader.ReadBuffer() 在 System.IO.StreamReader.ReadToEnd() 在 Sentry.Extensibility.DefaultRequestPayloadExtractor.DoExtractPayLoad(IHttpRequest 请求)在 Sentry.Extensibility.BaseRequestPayloadExtractor.ExtractPayload(IHttpRequest 请求)在 Sentry.Extensibility.RequestBodyExtractionDispatcher.ExtractPayload(IHttpRequest 请求)在 Sentry.AspNetCore.ScopeExtensions.SetBody(BaseScope 范围、HttpContext 上下文、SentryAspNetCoreOptions 选项)在 Sentry.AspNetCore.ScopeExtensions.Populate(范围范围,HttpContext 上下文,SentryAspNetCoreOptions 选项)在 Sentry.AspNetCore.SentryMiddleware.PopulateScope(HttpContext 上下文, 范围范围)在 Sentry.Scope.Evaluate()

代码隐藏

try
{
    string folder = "FileLocation/";
    string folderpath = "";

    if (model.Filess != null)
    {
        string fileExtension = Path.GetExtension(model.Filess.FileName);
        fileExtension = fileExtension.ToLower();

        long fileSize = model.Filess.Length;

        if (fileSize <= 10485760)
        {
            folderpath = Path.Combine(__hostingEnvironment.WebRootPath, "Uploads", folder);
            if (!Directory.Exists(folderpath))
            {
                Directory.CreateDirectory(folderpath);
            }

            var parsedContentDisposition =
                ContentDispositionHeaderValue.Parse(model.Filess.ContentDisposition);

            var filename = Path.Combine(__hostingEnvironment.WebRootPath,
                "Uploads", folder, parsedContentDisposition.FileName.Trim('"'));

            using (var stream = System.IO.File.OpenWrite(filename))
            {
                await model.Filess.CopyToAsync(stream);
            }
        }
    };
}
catch (Exception ex)
{

}

【问题讨论】:

    标签: c# asp.net .net iis asp.net-core


    【解决方案1】:

    根据这个 github issue,MSFT 已经在 asp.net core 2.1 上修复了这个问题。我建议您可以尝试在您的服务器上更新您的 asp.net 核心运行时版本(到 2.2)。

    我也在自己这边创建了一个测试demo并发布到IIS服务器上,效果很好。

    关于我的测试演示的更多细节,您可以参考以下代码:

    MVC 视图:

    @model  UploadViewModel
    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    
    
    <form asp-action="CreateAsync" enctype="multipart/form-data">
        <input asp-for="Id" /> 
        <input asp-for="Filess" />
        <input type="submit" />
    </form>
    

    控制器:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net.Http.Headers;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Mvc;
    using MVCCoreUpload.Models;
    
    namespace MVCCoreUpload.Controllers
    {
        public class FileUploadController : Controller
        {
    
            private readonly IHostingEnvironment _hostingEnvironment;
    
            public FileUploadController(IHostingEnvironment hostingEnvironment)
            {
                _hostingEnvironment = hostingEnvironment;
            }
    
            public IActionResult Index()
            {
                return View(new UploadViewModel());
            }
    
            public async Task<IActionResult> CreateAsync(UploadViewModel model)
            {
                try
                {
                    string folder = "FileLocation/";
                    string folderpath = "";
    
                    if (model.Filess != null)
                    {
                        string fileExtension = Path.GetExtension(model.Filess.FileName);
                        fileExtension = fileExtension.ToLower();
    
                        long fileSize = model.Filess.Length;
    
                        if (fileSize <= 10485760)
                        {
                            folderpath = Path.Combine(_hostingEnvironment.WebRootPath, "Uploads", folder);
                            if (!Directory.Exists(folderpath))
                            {
                                Directory.CreateDirectory(folderpath);
                            }
    
                            var parsedContentDisposition =
                                ContentDispositionHeaderValue.Parse(model.Filess.ContentDisposition);
    
                            var filename = Path.Combine(_hostingEnvironment.WebRootPath,
                                "Uploads", folder, parsedContentDisposition.FileName.Trim('"'));
    
                            using (var stream = System.IO.File.OpenWrite(filename))
                            {
                                await model.Filess.CopyToAsync(stream);
                            }
                        }
                    };
                }
                catch (Exception ex)
                {
    
                }
                return View("index");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 2021-08-02
      • 2018-09-14
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 2020-03-07
      相关资源
      最近更新 更多