用的SqlDependency 6的EntityFramework(异步) [英] SqlDependency with EntityFramework 6 (async)

查看:587
本文介绍了用的SqlDependency 6的EntityFramework(异步)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是EF 6 异步查询功能,如

I'm using the EF 6 async querying features, such as

var list = await cx.Clients.Where(c => c.FirstName.Length > 0).ToListAsync();

我想在这些查询也启动SQL依赖关系,这样我可以得到通知时,在数据库中的数据发生变化。我可以用这样做的 System.Runtime.Remoting.Messaging.CallContext 如下:

    async Task GetData()
    {
        using (ClientsContext context = new ClientsContext()) // subclass of DbContext
        {

            SqlDependency.Start(context.Database.Connection.ConnectionString);
            SqlDependency dependency = new SqlDependency();
            dependency.OnChange += (sender, e) =>
                {
                    Console.Write(e.ToString()); 
                };

            System.Runtime.Remoting.Messaging.CallContext.SetData("MS.SqlDependencyCookie", dependency.Id);
            var list = await context.Clients.Where(c => c.FirstName.Length > 0).ToListAsync();
        }
    }

..它工作正常。但是我遇到了一个问题,如果我想在多个查询有一个的SqlDependency 。如果我有类似两个异步方法的GetData()上面,和我在同时运行,只有第一个将得到更改通知。我认为这是由于具有饼干在继承每个方法设置Call​​Context中。如果我等待第一个异步的方法来完成,然后拨打第二个,他们都得到更改通知预期。有没有解决这个?

.. and it works fine. But I am running into an issue if I want to have an SqlDependency on more than one query. If I have two async methods similar to GetData() above, and I run both at the same time, only the first one will get change notifications. I assume that this is due to the CallContext having the cookie set by each method in succession. If I wait for the first async method to complete, then call the second, they both get change notifications as expected. Is there any solution to this?

推荐答案

我不是太熟悉的SqlDependency,但低于将使你的CallContext中有在ToListAsync被调用的时候(正确的值当多个呼叫正在运行)。概念证明这里, https://dotnetfiddle.net/F8FnFe

I'm not too familiar with SqlDependency, but the below will allow your CallContext to have the correct value at the time ToListAsync is called (When multiple calls are running). Proof of concept here, https://dotnetfiddle.net/F8FnFe

    async Task<List<Client>> GetData()
    {
        using (ClientsContext context = new ClientsContext()) // subclass of DbContext
        {
            SqlDependency.Start(context.Database.Connection.ConnectionString);
            SqlDependency dependency = new SqlDependency();
            dependency.OnChange += (sender, e) =>
            {
                Console.Write(e.ToString());
            };

            Task<List<Client>> task = Task<Task<List<Client>>>.Factory.StartNew(async () =>
            {
                System.Runtime.Remoting.Messaging.CallContext.SetData("MS.SqlDependencyCookie", dependency.Id);
                var list = await context.Clients.Where(c => c.FirstName.Length > 0).ToListAsync();
            }).Unwrap();

            return await task;
        }
    }

这篇关于用的SqlDependency 6的EntityFramework(异步)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆