gpt4 book ai didi

c# - 在 C# 中使用 BeginExecuteReader 设计异步数据提供程序

转载 作者:行者123 更新时间:2023-11-30 23:21:37 25 4
gpt4 key购买 nike

<分区>

我是 C# 5 中异步/等待模型的新手,所以您必须耐心等待我的无知/缺乏理解。

我想升级我的 Singleton 设计的数据提供程序以执行对存储过程的异步调用,然后使用 System.Data 命名空间中的 BeginDataReader 和 EndDataReader 方法返回数据。

这是我尝试构建但不等待数据返回的结构示例:

public class DataProvider{
private static DataProvider instance;
public static DataProvider Instance
{
get
{
if (instance == null)
{
lock (typeof(DataProvider))
{
instance = new DataProvider();
}
}
return instance;
}
}

public virtual async void ExecuteDataReaderAsync(string StoredProcedureName, AsyncCallback callback, params object[] Parameters)
{
InitDatabase();
var connection = new SqlConnection(databaseControllers[connectionStringName].ConnectionString);
var cmd = new SqlCommand();

cmd.Connection = connection;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = dbPrefixName + StoredProcedureName;
await connection.OpenAsync();

SqlCommandBuilder.DeriveParameters(cmd);
if (cmd.Parameters.Count - 1 > Parameters.Count())
throw new InvalidOperationException("The number of parameters provided does not match the number of parameters in the stored procedure. There are " + Parameters.Count().ToString() + " parameters provided, however the stored procedure requires " + cmd.Parameters.Count.ToString() + " parameters.");

for (int i = 0; i < Parameters.Count(); i++)
{
cmd.Parameters[i + 1].Value = Parameters[i];
}

cmd.BeginExecuteReader(new AsyncCallback(callback), cmd);
}
}

public class SubDataProvider : DataProvider
{
private static volatile SubDataProvider instance = new SubDataProvider();
public static SubDataProvider Instance
{
get
{
if (instance == null)
{
lock (typeof(SubDataProvider))
{
if (instance == null)
instance = new SubDataProvider();
}
}
return instance;
}
}

////
//// THIS IS WHERE I GET LOST
////

public async Task<List<Models.MyData>> GetDataAsync(bool IncludeDeleted = false)
{
List<Models.MyData> temp = new List<MyData>();
ExecuteDataReaderAsync("GetData", delegate (IAsyncResult result)
{

var database = (SqlCommand)result.AsyncState;

using (IDataReader reader = database.EndExecuteReader(result))
{
while (reader.Read())
{
temp.Add(FillData(reader));
}

}

if (database.Connection.State == ConnectionState.Open)
database.Connection.Close();

}, false);
return temp;
}
}


public class BusinessController
{
private static volatile BusinessController _instance = new BusinessController();

public static BusinessController Instance
{
get
{
if (_instance == null)
{
lock (typeof (BusinessController))
{
_instance = new BusinessController();
}
}
return _instance;
}
}

public async Task<List<Models.MyData>> GetAllAsync(bool IncludeDeleted = false)
{
return await SubDataProvider.Instance.GetDataAsync(IncludeDeleted);;
}
}


// DEMO
internal class Program
{
private static void Main(string[] args)
{
var x = BusinessController.Instance.GetAllAsync(false);
}
}

我的最终目标是将此数据返回到异步 WebApi,但我目前无法返回任何数据。 temp 变量被填充,但它从未实际返回填充的对象。

我哪里错了?

提前致谢!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com