gpt4 book ai didi

c# - 服务器端 AsyncPattern 调用 SQL Server

转载 作者:太空宇宙 更新时间:2023-11-03 11:38:15 24 4
gpt4 key购买 nike

我尝试在 WCF 数据服务中实现“AsyncPattern”。我在接口(interface)中定义了 2 个方法 BeginGetExperiments(...) 和 EndGetExperiments(...) 并实现了这些方法,如下所示。

public class GmdProfileService : IGmdProfileService
{
IAsyncResult IGmdProfileService.BeginGetExperiments(AsyncCallback callback, object state)
{
//IAsyncResult res = Experiment.GetExperimentsAsync(callback, state, Properties.Settings.Default.gmdConnectionString);
//return res;

System.Data.SqlClient.SqlConnectionStringBuilder csb = new System.Data.SqlClient.SqlConnectionStringBuilder(Properties.Settings.Default.gmdConnectionString);
csb.AsynchronousProcessing = true;
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(csb.ConnectionString);
conn.Open();
System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
cmd = conn.CreateCommand();
cmd.CommandText = "SELECT id, name, comment, date, doi FROM tf.TagList WITH(NOLOCK) WHERE proprietary=0;";
cmd.CommandType = System.Data.CommandType.Text;
return new SqlCommandAsyncResult(cmd, callback, state);
}

public List<Experiment> EndGetExperiments(IAsyncResult result)
{
List<Experiment> res = new List<Experiment>();
SqlCommandAsyncResult myresult = result as SqlCommandAsyncResult;
using (System.Data.SqlClient.SqlDataReader reader = myresult.cmd.EndExecuteReader(myresult.originalState as IAsyncResult))
{
try
{
while (reader.Read())
{
res.Add(new Experiment(reader));
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
// Closing the reader also closes the connection, because this reader was created using the CommandBehavior.CloseConnection value.
if (reader != null)
{
reader.Close();
}
}
}
return res;
}

BeginGetExperiments 返回一个实现 IAsyncResult 接口(interface)的类 SqlCommandAsyncResult,此外还保留对我的 SqlCommand 的引用以供以后访问。

public class SqlCommandAsyncResult : IAsyncResult
{
public SqlCommand cmd { get; private set; }
public IAsyncResult originalState { get; private set; }

public SqlCommandAsyncResult(SqlCommand cmd, AsyncCallback callback, object state)
{
this.cmd = cmd;
this.originalState = cmd.BeginExecuteReader(callback,
state,
System.Data.CommandBehavior.SequentialAccess | // doesn't load whole column into memory
System.Data.CommandBehavior.CloseConnection // close connection immediately after read
);
}

public object AsyncState
{
get { return originalState.AsyncState; }
}

public WaitHandle AsyncWaitHandle
{
get { return originalState.AsyncWaitHandle; }
}

public bool CompletedSynchronously
{
get { return false; }
}

public bool IsCompleted
{
get { return AsyncWaitHandle.WaitOne(0); }
}
}

我面临的困难在于 EndGetExperiments 方法。我不知道如何访问 SqlCommand 来调用 EndExecuteReader(...)。通常我会使用 BeginExecutereader 中的状态对象来传递命令。但如果我这样做,我会得到异常(exception):“IAsyncResult 的状态必须是传递给您的 Begin 调用的状态参数。”

所以我尝试使用 IAsyncResult 将 SqlCommand 转发给 EndGetExperiments。在这里,我不明白的一点是,在 EndGetExperiments 中,变量结果是 IAsyncResult 类型或 SqlCommandAsyncResult 类型,具体取决于值SqlCommandAsyncResult 类中的 CompletedSynchronously。设置 CompletedSynchronously = false 使我的代码失败,因为我无法访问 SqlCommand 而设置 CompletedSynchronously = true 代码就像一个魅力,但我有一种奇怪的感觉,某些东西可能在引擎盖下出错。

我感谢任何帮助、指导和示例代码如何使此代码工作,更重要的是帮助我理解手头的问题。

非常感谢。简

最佳答案

如今 WCF 数据服务不支持服务器上的异步处理。请在此处为其投票/添加功能请求:http://data.uservoice.com/forums/72027-wcf-data-services-feature-suggestions/topics/72603-wcf-data-services-feature-suggestions

关于c# - 服务器端 AsyncPattern 调用 SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5538860/

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