Task.Run参数在ASP MVC中不起作用但在没有参数的情况下工作

时间:2016-05-14 14:30:21

标签: c# asp.net-mvc multithreading iis-7

我有下面的代码,当该方法没有参数但在我传入参数时不起作用时可以工作。我最初认为当IIS中的上下文丢失时参数丢失了,所以我做了同样的事情的深层复制。我也尝试记录错误或消息,但同样的事情 - 它从不调用方法来完成任务。初始日志消息写成功,但不是我希望在asp mvc项目中添加的方法中的日志消息。我已经在这2天了,整个stackoverflow和谷歌没有解决方案。我添加了线程睡眠以使后台线程可能启动但它没有工作。 currentcontroller继承自basecontroller类。记录器使用log4net。日志工作,我看到数据库中的条目。添加了thread.sleep以查看它是否通过使父线程等待后台线程启动来解决问题。有什么想法吗?

public class MainController : BaseController
{
    [HttpPost]
    public JsonResult ProcessSynchronization(int sourceId, string[] cities)
    {
        var ret = false;
        try
        {              

            if (sourceId == 1)
            { 
                var cityDeep = (string[])cities.Clone();
                Logger.Info("ABout to start BeginSynchronization ..");

                Task.Factory.StartNew(() =>
                {
                    Logger.Info("ABout to start BeginSynchronization enter..");

                    BeginSynchronization(cityDeep);
                });


                Thread.Sleep(5000);
                ret = true;
            }

        }
        catch (Exception ex)
        {
            Logger.Error(LogUtility.BuildExceptionMessage(ex), ex);
            return Json(new { result = ret, responseText = "Customer could not be activated." });
        }
        return Json(new { result = ret, responseText = "Synchronization started." });
    }

    private void BeginSynchronization (string[] cities)
    {
        Logger.Info("BeginSynchronization entry processing..");

    }
}

public class BaseController : Controller
{
    /// <summary>
    /// Get logger
    /// </summary>
    protected ILog Logger
    {
        get { return LogManager.GetLogger(GetType()); }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用async / await关键字修改您的方法。

public async JsonResult ProcessSynchronization(int sourceId, string[] cities)
{
    var ret = false;
    try
    {              

        if (sourceId == 1)
        { 
            var cityDeep = (string[])cities.Clone();
            Logger.Info("ABout to start BeginSynchronization ..");

            await Task.Factory.StartNew(() =>
            {
                Logger.Info("ABout to start BeginSynchronization enter..");

                BeginSynchronization(cityDeep);
            });


            ret = true;
        }

    }
    catch (Exception ex)
    {
        Logger.Error(LogUtility.BuildExceptionMessage(ex), ex);
        return Json(new { result = ret, responseText = "Customer could not be activated." });
    }
    return Json(new { result = ret, responseText = "Synchronization started." });
}

没有async / await

public JsonResult ProcessSynchronization(int sourceId, string[] cities)
{
    var ret = false;
    try
    {              

        if (sourceId == 1)
        { 
            var cityDeep = (string[])cities.Clone();
            Logger.Info("ABout to start BeginSynchronization ..");

            var thread = new Thread(() =>
            {
                Logger.Info("ABout to start BeginSynchronization enter..");

                BeginSynchronization(cityDeep);
            });
            thread.Start();


            ret = true;
        }

    }
    catch (Exception ex)
    {
        Logger.Error(LogUtility.BuildExceptionMessage(ex), ex);
        return Json(new { result = ret, responseText = "Customer could not be activated." });
    }
    return Json(new { result = ret, responseText = "Synchronization started." });
}