2011-06-21 169 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

有一条评论说:“//这个异步实现的操作永远不会被调用,因为有一个评论同一版本的同一方法“。您必须决定您的服务幻灯片功能如何最好地实现 - 同步或异步。客户端可以独立地调用异步服务或同步服务,但与服务调用方式无关 - 仅与代理调用方式有关。如果你有两个,你会如何决定应该调用哪个版本? –