您的代码尝试调用不带任何参数的BeginUploadFromStream-Method的重载- 但没有这样的重载。为了从需要参数的异步方法创建 Task-Async-Pattern,请使用FromAsync<>-Method的通用版本。您可以在那里指定 Begin-Method 需要的参数。
对于 Begin/EndUploadFromStream 方法,代码如下所示:
Task.Factory.FromAsync<Stream>(blob.BeginUploadFromStream, blob.EndUploadFromStream, stream, null);
但是由于您也需要传递BlobRequestOptions,所以事情变得有点复杂:
接受 BlobRequestOptions 参数的 BeginUploadFromStream-Method 的重载,总共需要四个参数。但是 FromAsync<> 方法没有为 begin 方法接受三个以上输入参数的重载。
这是您可以使用 FromAsync<> 方法的方式:
为 ICloudBlob.BeginUploadFromStream 创建一个扩展方法,该方法需要两个参数(流和 BlobRequestOptions)并将它们正确传递给原始的 Begin-Method
public static class Extensions
{
public static IAsyncResult BeginUploadFromStream(this ICloudBlob blob, Stream source, BlobRequestOptions options, AsyncCallback callback, object state)
{
return blob.BeginUploadFromStream(source,
null,
options,
null,
callback,
state);
}
}
使用异步重载创建任务
Task.Factory
.FromAsync<Stream, BlobRequestOptions>(blob.BeginUploadFromStream,
blob.EndUploadFromStream,
stream,
options,
null);
如果您需要将所有四个参数传递给 BeginUploadFromStream-Method,请告诉我。我也知道如何解决这个问题。
更新:
这是一个四参数 Begin 方法的解决方案。我希望这个想法会产生更通用的东西并使用更少的反射,但现在它只适用于没有返回值的 EndMethod。
public static Task FromAsync<TArg1, TArg2, TArg3, TArg4>(this TaskFactory factory,
Func<TArg1, TArg2, TArg3, TArg4, AsyncCallback, object, IAsyncResult> beginMethod,
Action<IAsyncResult> endMethod,
TArg1 arg1,
TArg2 arg2,
TArg3 arg3,
TArg4 arg4,
object state,
TaskCreationOptions creationOptions)
{
// create a promise task that will wait until the async stuff is done
var constructor = typeof(Task).GetConstructor(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic, null, new Type[] { typeof(object), typeof(TaskCreationOptions), typeof(bool) }, null);
var promise = constructor.Invoke(new object[] { state, creationOptions, true }) as Task;
// perform the async pattern method
try
{
IAsyncResult asyncResult = beginMethod(arg1, arg2, arg3, arg4, delegate(IAsyncResult iar)
{
if (!iar.CompletedSynchronously)
{
// complete the async pattern
endMethod(iar);
var mFinish = typeof(Task).GetMethod("Finish", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
mFinish.Invoke(promise, new object[] { true });
}
}, state);
if (asyncResult.CompletedSynchronously)
{
// complete the async pattern synchronously
endMethod(asyncResult);
var mFinish = typeof(Task).GetMethod("Finish", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
mFinish.Invoke(promise, new object[] { true });
}
}
catch (Exception ex)
{
}
return promise as Task;
}