【问题标题】:Task.Factory.StarNew and ApplicationDbContext UpdateTask.Factory.StarNew 和 ApplicationDbContext 更新
【发布时间】:2017-09-21 00:43:09
【问题描述】:

我需要在任务中更新我的 ApplicationDbContext 表;但是,我没有完成它。这是我收到的错误消息:

ex.Message = "无法访问已处置的对象。常见原因 错误正在处理从依赖项解析的上下文 注入,然后尝试使用相同的上下文实例 在您的应用程序的其他地方。如果您拨打电话,可能会发生这种情况 丢弃...

我知道这与我不太熟悉的线程有关。

代码如下:

  [HttpPost]
        public ActionResult WebHook([FromBody] BotRequest data)
        {

         Task.Factory.StartNew(() =>
                {

                //Read value from Table
                ContextWatsonFB contextWatsonFB = _context.ContextWatsonFB.Where(m => m.RecipientId == recipientid).FirstOrDefault();

                if (contextWatsonFB == null)
                {
                    contextWatsonFB = new ContextWatsonFB()
                    {
                        RecipientId = recipientid
                    };
                    _context.Add(contextWatsonFB);
                    _context.SaveChanges();

                }
                else
                {
                    if (!string.IsNullOrEmpty(contextWatsonFB.Context))
                    {
                        model = JsonConvert.DeserializeObject<Context>(contextWatsonFB.Context);
                    }

                }

                ///DO SOME STUFF ////////////////

                ///Here I need to update my table using some values processed above in "some stuff"

                ContextWatsonFB contextWatsonFB = _context.ContextWatsonFB.Where(m => m.RecipientId == recipientid).FirstOrDefault();

                contextWatsonFB.Context = JsonConvert.SerializeObject(context);
                _context.Update(contextWatsonFB);
                _context.SaveChanges();

                }
        }

如您所知,它是 Facebook 的 webhook 连接,需要在任务中处理该过程。在“一些东西”中,基本上我正在使用 IBM Watson Conversation 服务,该服务坚持一个我无法来回发送到 Facebook 的对话“上下文”,这就是为什么我想将这些数据保存在一个表中保留来自 facebook messenger 的多个请求之间的差异。

【问题讨论】:

    标签: c# multithreading facebook watson-conversation


    【解决方案1】:

    幸运的是,下面的代码成功了:

            private readonly IServiceProvider _provider;
    
            public FacebookBotController(ApplicationDbContext context, IServiceProvider provider)
            {
                _provider = provider;
    
    }
    
      [HttpPost]
            public ActionResult WebHook([FromBody] BotRequest data)
            {
    
                if (data == null || data?.entry?.Count == 0)
                {
                    return new StatusCodeResult(StatusCodes.Status204NoContent);
                }
    
    
                try
                {
    
                    var task = Task.Factory.StartNew(async () =>
                    {
    
                        using (IServiceScope scope = _provider.GetRequiredService<IServiceScopeFactory>().CreateScope())
                        {
                            ApplicationDbContext _contx = _provider.GetService<ApplicationDbContext>();
    
                            ContextWatsonFB contextWatsonFB = await _contx.ContextWatsonFB.Where(m => m.SenderId == senderId).FirstOrDefaultAsync();
    
                            if (contextWatsonFB == null)
                            {
                                context = null;
                            }
                            else
                            {
                                context = JsonConvert.DeserializeObject<Context>(contextWatsonFB.Context);
                            }
    
                        }
    
    }
    }
    

    【讨论】:

    • 您可以添加解释,说明导致问题的原因,以及这段代码以何种方式解决了该问题。
    猜你喜欢
    • 1970-01-01
    • 2015-07-21
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 2017-07-28
    • 2016-07-15
    • 2015-05-17
    相关资源
    最近更新 更多