首页 > 解决方案 > System.Data.Services.Client.DataServiceContext 超时属性不起作用

问题描述

我遇到了一个查询在检索结果之前超时的问题。我正在使用实体框架 v. 6.0。设置 context.Timeout 属性似乎没有任何影响。

这是类声明和构造函数,所以你知道我在处理什么。它是从 Reference.cs 生成的代码:

    public partial class PSIDevEntities : global::System.Data.Services.Client.DataServiceContext
{
    /// <summary>
    /// Initialize a new PSIDevEntities object.
    /// </summary>
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
    public PSIDevEntities(global::System.Uri serviceRoot) : 
            base(serviceRoot, global::System.Data.Services.Common.DataServiceProtocolVersion.V3)
    {
        this.ResolveName = new global::System.Func<global::System.Type, string>(this.ResolveNameFromType);
        this.ResolveType = new global::System.Func<string, global::System.Type>(this.ResolveTypeFromName);
        this.OnContextCreated();
        this.Format.LoadServiceModel = GeneratedEdmModel.GetInstance;
    }

我尝试将超时设置为一个很高的数字。但是,查询会在 30 秒后超时。

var context = new PSIDevEntities(URI);
context.Timeout = 120000;
var result = from p in context.vwPacings
             where p.InactiveDateTime == null
             orderby p.ProgramTitle
             select p;
// timeout occurs here after 30 seconds
retList = result.ToList();

在某处,默认超时为 30 秒。我需要做些什么来增加此查询的超时时间?

标签: c#entity-framework

解决方案


答案是修改数据服务中自动生成的代码。哎呀!但它有效。更改数据服务的命令超时。将这两行输入到 DbContext 的构造函数中:

        var objectContext = (this as IObjectContextAdapter).ObjectContext;
        objectContext.CommandTimeout = 120;   // Or any required timeout

所以对于我的修复,我的构造函数现在看起来像这样:

public partial class PSIDevEntities : DbContext
{
    public PSIDevEntities()
        : base("name=PSIDevEntities")
    {
        base.Configuration.ProxyCreationEnabled = false;
        var objectContext = (this as IObjectContextAdapter).ObjectContext;
        objectContext.CommandTimeout = 120;
    }
...

推荐阅读