2
我有一個實用程序方法,用於在using語句中實例化datacontext。我想在異步方法調用中使用它,但是在方法返回之前將datacontext放置。什麼是使用這個的正確方法?如何將異步方法作爲Action或Func傳遞
這裏是方法(和重載)
public void Try(Action<IDataServices> method)
{
using (IDataServices client = GetClient())
{
method(client);
}
}
public TResult Try<TResult>(Func<IDataServices, TResult> method)
{
using (IDataServices client = GetClient())
{
return (TResult)method(client);
}
}
下面是我currenty使用它(失敗):
Model m = await ClientResolver.Try(async x => await x.GetModelByIDAsync(modelID));
參考:
Calling asynchronous method in using statement
你的'嘗試'應該採取'任務'。 – 2015-03-02 16:12:38