Click here to Skip to main content
15,890,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
Keith Barrow 2-May-11 12:32pm    
Are you sure it doesn't get called asynchronously? If the anonymous method is called then it most probably is. What makes you think it is being called synchronously? You don't close the channel BTW, it is a good plan to do so.
dan!sh 2-May-11 16:10pm    
Yes I am. I have a File.AppendText method call in the WCF operation just to check this. Nothing gets written to the file. Although the synchronous call does writes to the file and displays the result on the console.

On more thing is that even though I have Console.Read() method call, the console does not even waits for input. It just closes on async call.

Yes, I am closing the channel. Just didn't paste the code here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900