首页 新闻 赞助 找找看

Task.Factory.FromAsync 中丢失了 HttpContext.Current

0
悬赏园豆:30 [已解决问题] 解决于 2016-07-11 14:33

我是这样写的..

public async Task<string> SaveAsync(string modelStr)
        {
            var codeModel = JsonConvert.DeserializeObject<Model.GUT_CODE>(modelStr);
            await FillModelAsync(codeModel, false);
            codeModel.SerialModifiedTime = DateTime.Now;
            var result = await _bllCode.UpdateAsync(codeModel);
            //更新缓存, 不需要等待
            new CodeCacheManager(_bllCode).UpdateWholeCodeCacheItemAsync(codeModel);
            return result.ToString();
        }
private async Task FillModelAsync<T>(T model, bool isAdd) where T : BaseModel, new()
        {
            Action<T, bool> fillModel = (m, a) => CommonTool.FillModel(m, a);
            await Task.Factory.FromAsync(fillModel.BeginInvoke, fillModel.EndInvoke, model, isAdd, null);
        }

debug发现进CommonTool.FillModel方法新开了一个线程, 原来的线程丢弃了, 更加奇怪的是访问FillModel中访问数据库后用的是同一个新线程.

我想实现的是: 同一个线程一直用到访问数据库, 然后丢弃线程等待IO, IO完成后通知Asp.Net用新线程继续处理, 全程代码应该是可以访问到HttpContext的.

那么如何不新开线程呢? 求大家指点.

问题补充:

 貌似自己写的实现无法像微软的实现一样单线程, 不借助APM接口的话?

xiezhenhao的主页 xiezhenhao | 初学一级 | 园豆:100
提问于:2016-04-23 18:10
< >
分享
最佳答案
1

通过方法参数传递HttpContext

收获园豆:15
dudu | 高人七级 |园豆:31075 | 2016-04-23 19:05

有没有办法异步而不开新线程的吗

xiezhenhao | 园豆:100 (初学一级) | 2016-04-23 19:34

@xiezhenhao: 异步会造成线程切换,如果不想进行线程切换,就不要用异步。

dudu | 园豆:31075 (高人七级) | 2016-04-23 20:11

@dudu: 其实我关心的不是HttpContext没有传递, 而是调用异步方法时的效率和正确线程顺序.

看了这篇文章FromAsync(asyncResult, …) vs FromAsync(beginMethod, …), 以及MSDN上的解释, 

The FromAsync overloads that take an asyncResult parameter are not as efficient as the overloads that take a beginMethod parameter. If performance is an issue, use the overloads that provide the beginMethod/endMethod pattern.
The beginMethod delegate is started on the thread that FromAsync is running on. This method throws any exceptions thrown by the beginMethod.

似乎使用FromAsync(beginMethod, …)能解决我的问题.

xiezhenhao | 园豆:100 (初学一级) | 2016-04-23 20:39
其他回答(1)
2

task里面执行的方法和线程一样不能直接使用httpcontext,因为获取出来的就是null,这两种都需要通过参数传递当前线程的httpcontext进去,线程执行的方法才能获取到,也就是您这里必须通过model来传递一个httpcontext

收获园豆:15
神牛003 | 园豆:513 (小虾三级) | 2016-04-25 09:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册