简体   繁体   English

ASP.NET Core EF - 上传文件时出错

[英]ASP.NET Core EF - Getting an error when uploading a file

I am developing a system using ASP.NET Core, EF Core and my database are MySQL and I am using MySQL Connector.NET as the adapter.我正在开发一个使用 ASP.NET Core、EF Core 的系统,我的数据库是 MySQL,我使用 MySQL Connector.NET 作为适配器。

I have a place in my system where the user can choose a file and upload.我的系统中有一个地方供用户选择文件并上传。 My question is, when I try to upload a file which is more than 1000 KB, I am getting the following error:我的问题是,当我尝试上传超过 1000 KB 的文件时,出现以下错误:

An established connection was aborted by the software in your host machine已建立的连接被主机中的软件中止

The database field for this file is of a LONGBLOB type and the type of the modal is byte[] .该文件的数据库字段是LONGBLOB类型,模态类型是byte[] The error is thrown right inside the dbcontext.SaveChanges() method which is why it is hard to debug and identify the reason.该错误直接在dbcontext.SaveChanges()方法中抛出,这就是为什么很难调试和确定原因的原因。

I will post the full stack trace below:我将在下面发布完整的堆栈跟踪:

   at MySqlConnector.Protocol.Serialization.SocketByteHandler.WriteBytesAsync(ArraySegment`1 data, IOBehavior ioBehavior) in C:\projects\mysqlconnector\src\MySqlConnector\Protocol\Serialization\SocketByteHandler.cs:line 90
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySqlConnector.Utilities.ValueTaskExtensions.<ContinueWith>d__0`2.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\Utilities\ValueTaskExtensions.cs:line 8
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySqlConnector.Core.ServerSession.TryAsyncContinuation(Task`1 task) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 1225
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySqlConnector.Core.TextCommandExecutor.<ExecuteReaderAsync>d__1.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\Core\TextCommandExecutor.cs:line 36
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySql.Data.MySqlClient.MySqlCommand.<ExecuteNonQueryAsync>d__60.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 261
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 62
   at MySql.Data.MySqlClient.MySqlTransaction.Dispose(Boolean disposing) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlTransaction.cs:line 81
   at Microsoft.EntityFrameworkCore.Storage.RelationalTransaction.Dispose()
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(DbContext _, ValueTuple`2 parameters)
   at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Zoneberry.Repository.ZONContext.SaveChanges() in path\ZONContext.cs:line 157
   at path.Commit() in project\UnitOfWork.cs:line 419
   at project.Controller.UploadFileFor(List`1 files, Int32 leadId, Int32 totalCount) in project\Controllers\LeadController.cs:line 220
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()

The system works fine when I upload files which are smaller than 1MB.当我上传小于 1MB 的文件时,系统工作正常。

Probably you have a limit in a MySql side.可能您在MySql方面有限制。 Try to execute this command on MySql server:尝试在MySql服务器上执行此命令:

SHOW VARIABLES LIKE 'max_allowed_packet';

If the result is near 1 million bytes, then you need to increase that value (eg almost 2 MB) by running the following command:如果结果接近1 million字节,那么您需要通过运行以下命令来增加该值(例如几乎 2 MB):

SET GLOBAL max_allowed_packet=2000000;

But this setting will be reset after server reboot.但此设置将在服务器重新启动后重置。 To change the setting permanently you need to change it in my.ini or ~/.my.cnf under [mysqld] section:要永久更改设置,您需要在[mysqld]部分下的my.ini~/.my.cnf更改它:

max_allowed_packet=2M 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 IIS 上上传文件时,ASP.NET Core API 500 内部服务器错误 - ASP.NET Core API 500 Internal Server Error when uploading file on IIS 上传后在asp.net中出现错误 - getting error in asp.net after uploading 上载文件ASP.NET时生成错误消息 - generate error message when uploading a file ASP.NET 调用 asp.net 核心操作时出现 json 错误 - getting json error when invoking asp.net core action 两个连接ASP.NET Core、EF core、ArgumentNullException错误 - Two connections ASP.NET Core, EF core, ArgumentNullException error 实现 TodoList 服务时在 Asp.net 核心中出错 - Getting error in Asp.net core when implementing TodoList service 使用 Asp.NET Core 3.1 框架将文件上传到服务器时如何使用 IFormFile 作为属性? - How to use IFormFile as property when uploading file to a server using Asp.NET Core 3.1 framework? 上传超过 30 MB 的 ASP.NET Core 3.1 时出现 504 错误 - 504 error when uploading above 30 MB ASP.NET Core 3.1 在ASP.NET Core MVC中上传文件后立即读取文件 - Read a file immediately after uploading it in ASP.NET core MVC 上传和处理.csv文件到ASP.NET内核Web ZDB974238714CA8DE634A7CE1D08 - Uploading and handling .csv file to an ASP.NET Core Web API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM