Parallel.ForEach error HttpContext.Current

前端 未结 4 1418
猫巷女王i
猫巷女王i 2021-01-02 09:43

this method - doDayBegin(item.BranchId) is taking long time to execute. So I am using Parallel.ForEach to execute it parallel. When I am using norm

4条回答
  •  渐次进展
    2021-01-02 10:35

    HttpContext.Current is set per thread. So when you fire up more threads using Parallel.ForEach your new threads can't access it that way. The solution would be to pass the needed values as parameters all the way instead of relying on HttpContext.Current in your repositories.

    There are several sources here on SO that covers this problem already.

    The cross-thread usage of "HttpContext.Current" property and related things

    HttpContext.Current.Items in different thread

    Access HttpContext.Current from different threads

提交回复
热议问题