2011-06-21 94 views
0

我implemenented如下界面在我的WCF服務WCF客戶端沒有調用異步方法

[ServiceContract] 
public interface IPrepaidService 
{ 

    [OperationContract] 
    PrepaidSubscriberInfo GetSubscriberInfo(string ctn); 

    [OperationContractAttribute(AsyncPattern = true)] 
    IAsyncResult BeginGetSubscriberInfo(string ctn, AsyncCallback cb, object state); 
    PrepaidSubscriberInfo EndGetSubscriberInfo(IAsyncResult r); 



} 

這樣

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)] 
public class PrepaidService : IPrepaidService 
{ 

     public PrepaidSubscriberInfo GetSubscriberInfo(string ctn) 
     { 

      PrepaidSubscriberInfo result = null; 

      try 
      { 
     result = new PrepaidSubscriberInfo(); 
     ... 

      } 
      catch (Exception ex) { throw new Exception(ex.ToString(), ex); } 
      return result; 
     } 

     public IAsyncResult BeginGetSubscriberInfo(string ctn, AsyncCallback cb, object state) 
     {    
      Task<PrepaidSubscriberInfo> task = Task<PrepaidSubscriberInfo>.Factory.StartNew(_ => GetSubscriberInfo(ctn), state); 
      if (cb != null) task.ContinueWith(res => cb(task)); 
      return task; 
     } 

     public PrepaidSubscriberInfo EndGetSubscriberInfo(IAsyncResult ar) 
     {    
      return ((Task<PrepaidSubscriberInfo>)ar).Result; 
     } 
} 

我生成的代理和配置文件:

c:\ temp> svcutil http://localhost/Service.svc/async

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="MetadataExchangeHttpBinding_IPrepaidService" closeTimeout="00:01:00" 
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
      bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
      maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
      messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" 
      allowCookies="false"> 
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
       maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      <reliableSession ordered="true" inactivityTimeout="00:10:00" 
       enabled="false" /> 
      <security mode="None"> 
      <transport clientCredentialType="Windows" proxyCredentialType="None" 
       realm="" /> 
      <message clientCredentialType="Windows" negotiateServiceCredential="true" /> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost/Service.svc" 
      binding="wsHttpBinding" bindingConfiguration="MetadataExchangeHttpBinding_IPrepaidService" 
      contract="IPrepaidService" name="MetadataExchangeHttpBinding_IPrepaidService"> 
     <identity> 
      <dns value="localhost" /> 
     </identity> 
     </endpoint> 
    </client> 
    </system.serviceModel> 

我試圖調用WCF客戶端異步方法,這樣

static void Main(string[] args) 
    { 

     ChannelFactory<IPrepaidServiceChannel> factory = new ChannelFactory<IPrepaidServiceChannel>("MetadataExchangeHttpBinding_IPrepaidService"); 
     factory.Open(); 
     IPrepaidServiceChannel channelClient = factory.CreateChannel(); 

     IAsyncResult arAdd = channelClient.BeginGetSubscriberInfo("xxx", AddCallback, channelClient); 

     IAsyncResult arAdd2 = channelClient.BeginGetSubscriberInfo("xxx", AddCallback, channelClient); 

     IAsyncResult arAdd3 = channelClient.BeginGetSubscriberInfo("yyy", AddCallback, channelClient); 

     IAsyncResult arAdd4 = channelClient.BeginGetSubscriberInfo("yyy", AddCallback, channelClient); 
     Console.WriteLine("1"); 



     Console.ReadKey(); 

    } 

    static void AddCallback(IAsyncResult ar) 
    { 
     var result = ((IPrepaidService)ar.AsyncState).EndGetSubscriberInfo(ar); 
     Console.WriteLine("Result: {0}", result); 
    } 

但WCF客戶端總是調用WCF服務,而不是它的異步版本 BeginGetSubscriberInfo的GetSubscriberInfo()方法和EndGetSubscriberInfo。當我刪除[OperationContract] PrepaidSubscriberInfo GetSubscriberInfo(string ctn); 客戶端調用異步版本。

壞格式對不起球員,但我不能用這個UI

+1

「對不起格式不好的人,但我無法用這個用戶界面進行管理」:是不是很難選擇你的代碼並點擊腳本塊圖標? –

+1

@Steve B.我試圖做到這一點,但它嚴重格式化我的代碼 –

回答

1

客戶端和服務管理可以執行/不能獨立執行異步。客戶端調用異步僅僅意味着從客戶端角度來看,調用是異步的,服務甚至不必支持異步

服務將優先於異步同步,所以如果您希望您的服務被調用異步,則刪除同步版本從它的合同版本

+0

如果我想在我的合同中有兩個版本,該怎麼辦?例如,這裏http://msdn.microsoft.com/en-us/library/ms731177.aspx被聲明爲兩個版本 –

+2

有一條評論說:「//這個異步實現的操作永遠不會被調用,因爲有一個評論同一版本的同一方法「。您必須決定您的服務幻燈片功能如何最好地實現 - 同步或異步。客戶端可以獨立地調用異步服務或同步服務,但與服務調用方式無關 - 僅與代理調用方式有關。如果你有兩個,你會如何決定應該調用哪個版本? –