MVC - System.Threading.ThreadAbortException in a parallel task

In my MVC application, a super administrator can set up a task queue, such as updating a database. Thus, when an administrator adds an update to the queue, the controller starts new tasks that run in the background. However, when you add several tasks, the application throws System.Threading.ThreadAbortException: Thread was being aborted. Moreover, stacktrace assumes this happens on different lines of code.

I should also add that tasks use EF6 objects to work with the sql server, and according to the stack trace, this happens after or during database operations. Since the updates are usually large, I use db.Configuration.AutoDetectChangesEnabled = falseand manually save the changes every 20 thousand lines, arranging and re-creating the database.

Stack trace example:

5:18:36 PM Wednesday, July 15, 2015: [REPORT] Exception (line: 456667; section6): System.Threading.ThreadAbortException: thread terminated. in System.Array.Copy (Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable) in System.Collections.Generic.List`1.set_Capacity (Int32 value) in System.Data.Entity.Core. Metadata.Edm.MetadataCollection`1.SetReadOnly () in System.Data.Entity.Core.Metadata.Edm.TypeUsage..ctor (EdmType edmType, IEnumerable`1 facets) in System.Data.Entity.Core.Common.CommandTrees. DbExpression..ctor (type DbExpressionKind, type TypeUsage, boolean forceNullable) in System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder.DbExpressionBuilder.PropertyFromMember (DbExpression instance, EdmMember property, StringArgumentName property) in System.Data.Entity.Core.Mapping.Update.Internal.UpdateCompiler.GenerateEqualityExpression (DbExpressionBinding target, EdmProperty property, PropagatorResult value) in System.Data.Entity.Core.Mapping.Update.Internal.UpdateCompiler.BuildPredicate, PropagentRegrentagentpropagate, reference current, TableChangeProcessor processor, Boolean & rowMustBeTouched) in System.Data.Entity.Core.Mapping.Update.Internal.UpdateCompiler.BuildUpdateCommand (PropagatorResult oldRow, PropagatorResult newRow, tableChangeProcessor processor). Internal.TableChangeProcessor.CompileCommands (ChangeNode changeNode, UpdateCompiler compiler) in System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.d__a.MoveNext () in System.Linq.Enumerable.d__71`1.MoveNext () in System .Data.Entity.Core.Mapping.Update.Internal.UpdateCommandOrderer..ctor (IEnumerable`1 commands, UpdateTranslator translator) in System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ProduceCommands () in System.Data.Entity. Core.Mapping.Update.Internal.UpdateTranslator.Update () in System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction [T] (Func`1 func, IDbExecutionStrategy executeStrategy, Boolean startLocalTransaction, BooleanataConn. Entity.Core.Objects.ObjectContext.SaveChangesToStore (parameters SaveOptions, IDbExecutionStrategy executeStrategy, logical startLocalTransaction) in System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute [TResult] in System Funcore.tb.nts.nts.nts.ntb.nts.nts.nts.nts.nts.nts.ntb.nts.nts.nts.nts.ntb.nts.nts.nts.ntb.nts.ntb.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.nts.ntject.ts .ObjectContext.SaveChangesInternal (SaveOptions options,Boolean executeInExistingTransaction) in System.Data.Entity.Internal.InternalContext.SaveChanges () on MyWebsite.Controllers.AdminPanelController.ApplyUpdate (String filePath, HttpApplicationStateBase context, Int32 saveInterval, logical checkRCollisions, logical checkRepetitions)

-, ?

+4
2

, IIS. , IIS , . IIS . 10 :

public void KeepIisBackgroundThreadsAlive(Object stateInfo) {
    while (true) {
        var serverOwnIp = Dns.GetHostEntry(Dns.GetHostName()).AddressList.First(o => o.AddressFamily == AddressFamily.InterNetwork).ToString();
        var req = (HttpWebRequest) WebRequest.Create(new Uri("http://" + serverOwnIp));
        req.Method = "GET";
        var response = (HttpWebResponse) req.GetResponse();
        var respStream = response.GetResponseStream();
        var delay = new TimeSpan(0, 0, 10, 0);
        Thread.Sleep(delay);
    }
}

ThreadPool.QueueUserWorkItem Global.asax.cs OnApplicationStarted:

protected override void OnApplicationStarted(){
    ThreadPool.QueueUserWorkItem(KeepIisBackgroundThreadsAlive)
}

, IIS. , IIS. , 100% , .

, -, 1- . -, , , IIS ( Azure Web Role - , ).

+4

, , , Windows Service. Daemon, / , 4 . , .

, , ; @Vova , IIS, IIS , , . .

, (Google , ):

EDIT: , , , Daemon , .

, . "" , . , , , - , , . , . , , :

  • : , . x y% ui . ( ), ...
  • : , , ,
  • CallOnce: , , .

MongoDB, ... , , , ( , , /).

, , , (, ..). , WCF, 2 :

- , ( KnockoutJS) - , Daemon.

, ( ) backend, . Solr , , , .

+2

Source: https://habr.com/ru/post/1598264/


All Articles