c# - 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 秒。我需要做些什么来增加此查询的超时时间?
解决方案
答案是修改数据服务中自动生成的代码。哎呀!但它有效。更改数据服务的命令超时。将这两行输入到 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;
}
...
推荐阅读
- c# - 如何在我的应用程序后台初始化 UrhoSharp?
- css - WordPress 主题上的水平滚动仅在 Firefox 上
- visual-studio - SSIS 项目:[文件] 无法打开或加载,因为它已在另一个项目或解决方案中打开或加载
- python - 不允许使用属性 debugOptions
- python - 按时间间隔分列并计算总和
- javascript - Chrome 存储同步设置并无法正常工作
- javascript - 如何在我网站上的页面之间发送数据
- excel - 如何使用硒包装器从带有 vba 的 html 中获取特定元素
- unity3d - 实例化移动的原始对象
- android - 在没有 findViewByID 的情况下访问视图?