我正在进行Async webservice调用,正在玩这个Task并等待构建:
private static async Task<RSAParameters> GetPublicSecretKey(ICoreIdentityService identityChannel)
{
Object state = null;
var t = Task<RSAParameters>.Factory.FromAsync(
identityChannel.BeginGetPublicKey,
identityChannel.EndGetPublicKey,
null, state, TaskCreationOptions.None);
return await t;
}
//Methods definition:
//IAsyncResult BeginGetPublicKey(AsyncCallback callback, object asyncState)
//RSAParameters EndGetPublicKey(IAsyncResult result)
构建我得到的代码方法的类型参数....不能从用法中推断出来。我错过了什么吗?
提前谢谢。干杯,inoel
修改后的编译代码:
var t = Task<RSAParameters>.Factory.FromAsync(
identityChannel.BeginGetPublicKey,
identityChannel.EndGetPublicKey,
TaskCreationOptions.None);
答案 0 :(得分:2)
您似乎正在以意外的顺序使用您的参数调用FromAsync()方法。
错误消息本身建议明确命名您的参数,因此您的代码将看起来像这样:
var t = Task<RSAParameters>.Factory.FromAsync(
asyncResult: identityChannel.BegineGetPublicKey,
endMethod: identityChannel.EndGetPublicKey,
creationOptions: TaskCreationOptions.None,
scheduler: state);
或者,您可以更正参数的顺序,这应该可以解决问题。 我能找到的最近的重载是:
public Task<TResult> FromAsync<TArg1, TArg2>(Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod, TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions);
因此,假设您打算使用此代码,则需要稍微修改代码以传递arg1和arg2的类型,然后传入一个附加参数:
Object state = null;
var t = Task<RSAParameters>.Factory.FromAsync<TArg1, TArg2>(
beginMethod: identityChannel.BeginGetPublicKey,
endMethod: identityChannel.EndGetPublicKey,
arg1: null, // Either arg1, or arg2 is missing
arg2: null, // from your code
state: state,
creationOptions: TaskCreationOptions.None);
return t;
为了清楚起见,我在这里留下了命名参数,但如果您愿意,您应该可以删除它们。