2014-10-10 157 views
1

我有以下問題:調用WCF方法異步

我的WCF的方法是這樣的

public TransferResult<bool> ExecuteMyMethod(string jobName, 
            Collection<KeyValuePair<string, string>> parameters) 
{ 
    do something; 
} 

相應的合同:

[OperationContract] 
TransferResult<bool> ExecuteMyMethod(string jobName, 
            Collection<KeyValuePair<string, string>> parameters); 

現在,在我們的項目中存在的代理用你可以異步調用方法的包裝器,我必須使用它:

public void InvokeAsync<TArg1, TArg2, TResult>(
    Expression<Func<IMyServiceClient, Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult>>> beginMethod, 
    Expression<Func<IMyServiceClient, Func<IAsyncResult, TransferResult<TResult>>>> endMethod, 
    TArg1 arg1, 
    TArg2 arg2, 
    Action<Task<TransferResult<TResult>>> continuationAction) 
{ 
    do something; 
} 

當我嘗試調用我的方法是這樣的:

this.wrapper.InvokeAsync<string, Collection<KeyValuePair<string, string>>, bool>(
       svc => svc.BeginExecuteMyMethod, 
       svc => svc.EndExecuteMyMethod, 
       "jobname", 
       parameters, 
       this.ContinuationAction); 

的VS不斷告訴我:

預計與「IAsyncResult的BeginExecuteMyMethod(字符串, 收藏>,AsyncCallback的,對象的方法)' 簽名

你能告訴我我做錯了什麼嗎? 參數參數的類型是集> 的我很新的WCF和真的不知道是什麼問題:(

在此先感謝

編輯

我驗證了屬性[OperationContractAttribute(AsyncPattern=true)]在服務引用

設置在BeginExecuteMyMethod方法EDIT2 這就是微博在IMyServiceClient的DY看起來像

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IMyService/ExecuteMyMethod", ReplyAction="http://tempuri.org/IMyService/ExecuteMyMethod")] 
TransferResult<bool> ExecuteMyMethod(string jobName, System.Collections.ObjectModel.Collection<MyService.KeyValuePairOfstringstring> parameters); 

[System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://tempuri.org/IMyService/ExecuteMyMethod", ReplyAction="http://tempuri.org/IMyService/ExecuteMyMethod")] 
System.IAsyncResult BeginExecuteMyMethod(string jobName, System.Collections.ObjectModel.Collection<MyService.KeyValuePairOfstringstring> parameters, System.AsyncCallback callback, object asyncState); 
TransferResult<bool> EndExecuteMyMethod(System.IAsyncResult result); 
+0

您需要提供[異步版本(http://msdn.microsoft.com/en-us/library/ms731177(V = vs.110)的.aspx)你的'ExecuteMyMethod'方法。 – Michael 2014-10-10 12:58:35

+0

嘿邁克爾感謝您的評論。我只是在引用中驗證了此屬性已設置:'[System.ServiceModel.OperationContractAttribute(AsyncPattern = true,Action =「.....」)]'服務引用設置爲「允許生成異步操作 - >生成異步操作「,它自動添加了屬性 – xeraphim 2014-10-10 13:02:30

+0

您可以發佈您的'IMyServiceClient'的身體嗎? – Michael 2014-10-10 13:14:13

回答

1

看那BeginExecuteMethod,參數是收藏<KeyValuePairOfstringstring>,但如果你看看你的包裝,你打電話InvokeAsync與不同類型的收集的類型< KeyValuePair <字符串,字符串> >

BeginExecute方法

IAsyncResult BeginExecuteMyMethod(string jobName, Collection<KeyValuePairOfstringstring> parameters, AsyncCallback callback, object asyncState); 

InvokeAsync調用

this.wrapper.InvokeAsync<string, Collection<KeyValuePair<string, string>>, bool>(
      svc => svc.BeginExecuteMyMethod, 
      svc => svc.EndExecuteMyMethod, 
      "jobname", 
      parameters, 
      this.ContinuationAction);