The book I am reading says I can call a operation in async fashion without doing anything in the service side.
So, I call a WCF operation using ChannelFactory in asynchronous way. I created a WCF service and have not made any changes to the sample GetData operation that VS creates apart from adding a
File.ApendAllText
. In the client side, I have both synchronous and asynchronous operation contracts defined. Here is how it looks in proxy class:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference.IService1")]
public interface IService1 {
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetData", ReplyAction="http://tempuri.org/IService1/GetDataResponse")]
SampleClient.ServiceReference.GetDataResponse GetData(SampleClient.ServiceReference.GetDataRequest request);
[System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://tempuri.org/IService1/GetData", ReplyAction="http://tempuri.org/IService1/GetDataResponse")]
System.IAsyncResult BeginGetData(SampleClient.ServiceReference.GetDataRequest request, System.AsyncCallback callback, object asyncState);
SampleClient.ServiceReference.GetDataResponse EndGetData(System.IAsyncResult result);
}
I have the following code in the client side (console application):
ChannelFactory<IService1Channel> sampleServiceChannel = new ChannelFactory<IService1Channel>(new BasicHttpBinding(), new EndpointAddress("http://localhost:57632/Service1.svc"));
IService1 sampleService = sampleServiceChannel.CreateChannel();
Console.WriteLine(sampleService.GetData(new GetDataRequest(3)).GetDataResult);
sampleService.BeginGetData(
new GetDataRequest(2),
delegate(IAsyncResult asyncResult)
{
Console.WriteLine(sampleService.EndGetData(asyncResult).GetDataResult);
Console.Read();
},
null);
I think I am missing something here. Can anyone point out what it is?
There is no error but operation does not gets called the async way. With same Channel, I can call the operation synchronously.