【问题标题】:Async/Await with Entity Framework 6.1.1 and impersonationAsync/Await 与 Entity Framework 6.1.1 和模拟
【发布时间】:2014-10-07 07:37:04
【问题描述】:

我有一个托管在 IIS 中的 WCF 服务,它从多个源(所有 SQL Server)检索数据。对于每个数据源,我必须模拟不同的 Active Directory 用户来连接到数据库。我将实体框架 v6.1.1 用于两个数据源。连接字符串中的 Integrated Security 也设置为 True。

我使用下面的示例来设置模拟用户,其中模拟用户是我从配置中设置的System.Security.Principal.WindowsImpersonationContext

internal async Task<List<string>> GetItemsByLookupItemsAsync(List<string> lookupItems) 
{
    var result = new List<string>();

    using (var db = new EntityFrameworkDb()) 
    {

        var query = from item in db.Table
                    where lookupItems.Contains(item.LookupColumn)
                    select item.StringColumn;

        var queryResult = new List<string>();
        using (GetImpersonatedUser())
        {
            queryResult.AddRange(await query.ToListAsync());
        }

        result.AddRange(queryResult.OrderBy(e => e));
    }

    return result;
}

问题是前面的代码抛出了SqlException,表示运行web服务的账号无法登录数据库。看来,当我点击 await 时,我失去了模拟上下文。

有什么建议可以解决这个问题?

【问题讨论】:

  • 我通常的建议是不要对数据库调用使用异步,因为它几乎从来没有做任何好事。
  • @usr 你能详细说明一下吗?
  • @YuvalItzchakov 我有:stackoverflow.com/a/25087273/122718 如果你怀疑这个说法,试着清楚地表达为什么使用异步 IO 调用数据库是有帮助的。很难找到原因。
  • @usr stephan 的最新评论怎么样?关于能够比以前更好地扩展的数据库类型。如果不是太贵,为什么不利用呢?

标签: c# .net sql-server entity-framework async-await


【解决方案1】:

web.config 中将legacyImpersonationPolicy 设置为false,将alwaysFlowImpersonationPolicy 设置为true 并重新启动IIS

<configuration>
   <runtime>
     <legacyImpersonationPolicy enabled="false"/>
    <alwaysFlowImpersonationPolicy enabled="true"/>   
  </runtime>
</configuration>

【讨论】:

  • 这在 web.config 中的 IIS 和我的客户端 app.config 中都有效。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-18
  • 2021-11-13
  • 1970-01-01
相关资源
最近更新 更多