c# - Enterprise Library 5.0 和命令超时

标签 c# timeout command enterprise

我正在使用以下代码,想知道如果使用企业库的 CreateSprocAccessor 是否需要设置命令超时,如果不需要,那么如何管理超时?

var accessor = _sqlDatabase.CreateSprocAccessor<xyz>("uspGetxyz", 
                     new xyzParameters(_sqlDatabase),
                     MapBuilder<xyz>.MapAllProperties().Build());

//Execute the accessor to obtain the results
var Data = accessor.Execute();
xyzList = Data.ToList<xyz>();

最佳答案

我很久以前就开始使用 Microsoft Enterprise Library,在正常情况下,数据库操作调用使用“数据库”类提供的方法来满足需要。在某些情况下,对于长时间运行的查询,开发人员希望设置 SqlCommand(或 DbCommand)类的 CommandTimeout 属性。这将允许查询按照命令超时中设置的值执行很长时间。

默认情况下,数据访问应用程序 block 不支持/在方法调用中采用简单的 CommandTimeout 参数(网络上有许多解决方法示例)。为了以最少的更改实现相同的效果,我在“Microsoft.Practices.EnterpriseLibrary.Data.Database”类中添加了一个名为“WithCommandTimeOut”的简单函数,该函数采用 timeOutSecond 参数,它返回“Database”类的相同实例。请参阅下面更新的代码片段以了解代码更改。希望这会解决超时问题。

//Class Level Static Variables
//Used to reset to default after assigning in "PrepareCommand" static method
static int DEFAULT_COMMAND_TIMEOUT_RESET = 30;

//Default value when "WithCommandTimeOut" not called
static int COMMAND_TIMEOUT_FOR_THIS_CALL = DEFAULT_COMMAND_TIMEOUT_RESET; 

public Database WithCommandTimeOut(int timeOutSeconds)
{
    COMMAND_TIMEOUT_FOR_THIS_CALL = timeOutSeconds;
    return this;
}

protected static void PrepareCommand(DbCommand command, DbConnection connection)
{
    if (command == null) throw new ArgumentNullException("command");
    if (connection == null) throw new ArgumentNullException("connection");

    //Here is the magical code ----------------------------
    command.CommandTimeout = COMMAND_TIMEOUT_FOR_THIS_CALL;
    //Here is the magical code ----------------------------

    command.Connection = connection;

    //Resetting value to default as this is static and subsequent
    //db calls should work with default timeout i.e. 30
    COMMAND_TIMEOUT_FOR_THIS_CALL = DEFAULT_COMMAND_TIMEOUT_RESET;
}

例。 数据库 db = EnterpriseLibraryContainer.Current.GetInstance(Of Database)("SmartSoftware"); db.WithCommandTimeOut(0).ExecuteDataSet(CommandType.Text, query);

关于c# - Enterprise Library 5.0 和命令超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6030297/

相关文章:

Linux在命令行中逐像素创建图像

linux - 在 CSV 的列中搜索模式并使用 sed 命令替换同一行中的另一个模式

c# - 为 .NET Web 服务设置超时值

c# - 收集流程中的错误并将它们发送回调用者 - 最佳方法?

c# - Web 作业上的 Windows Azure 管理库认证错误

c# - 如何使我的代码快速

java - 表单提交的异常长的后端处理对浏览器的影响是什么以及如何处理?

php - 如何在不超时的情况下解析大型 CSV 文件?

silverlight - MVVM:命令与事件处理程序

c# - 无法在 WPF MVVM 中将 View 绑定(bind)到 ViewModel