我正在学习如何将CCR(并发和协调运行时)与异步WCF Web服务结合使用。在异步WCF服务中使用CCR
这是测试WCF服务:
public class Service : IService
{
private Accounts.Manager accountManager = new Accounts.Manager();
public IAsyncResult BeginGetAccount(int id, AsyncCallback callback, object state)
{
//How Do I Call the CCR Function without blocking a Thread?
throw new NotImplementedException();
}
public string EndGetAccount(IAsyncResult result)
{
//How Do I Finish the Call and Pass back the Result?
throw new NotImplementedException();
}
}
这将需要一个ID号,并返回匹配的帐户名(如果有的话)
我已经写了CCR功能应该找到匹配的帐户(s) (显然需要大量的工作 - 这只是概念验证) 这里是我去的地方。
如何传回结果(全局端口?) 更重要的是:如何在不阻塞线程的情况下将CCR插入WCF异步服务调用?
public IEnumerator<ITask> GetAccount(int id)
{
SqlDataReader reader = null;
SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=BizData;Integrated Security=True;Async=True;");
string query = "SELECT * FROM Account WHERE AccountID = @AccountID";
SqlCommand command = new SqlCommand(query, connection);
SqlParameter accountID = new SqlParameter("AccountID", id);
command.Parameters.Add(accountID);
connection.Open();
yield return Arbiter.Choice(SQLAdapter.GetReader(command),
delegate(SqlDataReader r) { reader = r; },
delegate(Exception e) { Console.Write("Failed to get SQL data"); });
if (reader == null) yield break;
while (reader.Read())
{
Account account = new Account { ID = Convert.ToInt32(reader["AccountID"]),
Name = reader["Account"].ToString(),
ParkingNo = Convert.ToInt32(reader["ParkingNo"]),
Password = reader["Password"].ToString() };
//Post account?
}
connection.Close();
}