I implemenented the following interface in my WCF Service
[ServiceContract]
public interface IPrepaidService
{
[OperationContract]
PrepaidSubscriberInfo GetSubscriberInfo(string ctn);
[OperationContractAttribute(AsyncPattern = true)]
IAsyncResult BeginGetSubscriberInfo(string ctn, AsyncCallback cb, object state);
PrepaidSubscriberInfo EndGetSubscriberInfo(IAsyncResult r);
}
this way
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class PrepaidService : IPrepaidService
{
public PrepaidSubscriberInfo GetSubsc开发者_StackOverflowriberInfo(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;
}
}
I generated the proxy and config file:
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>
I'm trying to call asynchronous methods on the WCF client this way
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);
}
but the WCF client is always calling the GetSubscriberInfo() method on the WCF Service instead of its asynchronous version BeginGetSubscriberInfo and EndGetSubscriberInfo. When I remove [OperationContract] PrepaidSubscriberInfo GetSubscriberInfo(string ctn); the client calls asynchronous version.
Sorry guys for bad formatting but I couldn't manage with this UI
The client and service can perform/not perform async independently. The client calls async simply means from the client perspective the calls are async the service doesn't even have to support async
The service will prefer sync over async so if you want your service to be invoked async then remove the sync version from its version of the contract
精彩评论