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

时间:2019-02-05 11:40:00

标签: c# mysql entity-framework asp.net-core

我正在使用ASP.NET Core,EF Core开发系统,而我的数据库是MySQL,并且正在使用MySQL Connector.NET作为适配器。

我的系统中有一个用户可以选择文件并上载的地方。我的问题是,当我尝试上传大于1000 KB的文件时,出现以下错误:

  

已建立的连接被主机中的软件中止了

此文件的数据库字段为LONGBLOB类型,模态类型为byte[]。该错误直接抛出在dbcontext.SaveChanges()方法内部,这就是为什么难以调试和确定原因的原因。

我将在下面发布完整的堆栈跟踪:

   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()

当我上传小于1MB的文件时,系统运行正常。

1 个答案:

答案 0 :(得分:1)

MySql端可能有一个限制。尝试在MySql服务器上执行以下命令:

SHOW VARIABLES LIKE 'max_allowed_packet';

如果结果接近1 million个字节,则需要通过运行以下命令来增加该值(例如,将近2 MB):

SET GLOBAL max_allowed_packet=2000000;

但是服务器重启后此设置将被重置。 要永久更改设置,您需要在my.ini部分下的~/.my.cnf[mysqld]中进行更改:

max_allowed_packet=2M