how to make Async version of a method (not using VS plugin)

Oct 6, 2009 at 11:41 AM

Hi again, i know the VS plugin makes the Async version of methods call, but we are a big group of developers and its not easy to change the VS config to me yet and everybody does to, so, im using your ExceptionHandlingProxyBase.cs directly in some project and im making the Proxy as the tutorial video shows, some like:

public CorporateUser GetUser(string userId)
{
    return base.Invoke<CorporateUser>("GetUser", userId);
}

How should i do a Async version of this method ? (as an example)

I know i can try see generated code and try some similar but would be better if you can post it to all people checking this project.

 

Thanks again.

Enrique

 

Apr 13, 2010 at 1:37 PM

Hello, im using Michel's great plugin and i also want to know how to use async calls becouse the proxy doesnt generate them is this correct?

Apr 21, 2010 at 4:02 PM
Edited Apr 21, 2010 at 4:10 PM

Hi,

To get the asynchronous methods, simply go into advanced when you create the reference or right click it and select configure. In that dialog, tick "Generate asynchronous operations" and you will get Begin/End-methods automatically.

Something like this should work in my mind;

	public class AsyncCallDemo
	{
		private ServiceReference1.SomeServiceProxy proxy;
		private SynchronizationContext sync;

		public AsyncCallDemo()
		{
			sync = SynchronizationContext.Current;
			proxy = new ServiceReference1.SomeServiceProxy();
		}

		private Dictionary<int, Action<ServiceReference1.Something>> callbacks;
		private int stateId = 0;
		public void GetSomething(ServiceReference1.SomethingKey id, Action<ServiceReference1.Something> callback)
		{
			callbacks.Add(stateId, callback);
			proxy.BeginGetSomething(id, new AsyncCallback(GetSomethingCompleted), stateId++);
		}

		private void GetSomethingCompleted(IAsyncResult ar)
		{
			sync.Send((o) => callbacks[(int)ar.AsyncState](proxy.EndGetSomething(o as IAsyncResult)), ar);
		}
	}
This code snippet includes handling of synchronization with the UI thread, that you need if you are doing WPF and some provisional support for multiple parallell calls.
I haven't tested this, but this should work:
	AsyncCallDemo demo = new AsyncCallDemo();
	demo.GetSomething(someVariable, (something) => Console.WriteLine(something.ToString()));
Cheers, Morten
Sep 12, 2011 at 4:34 PM

Hi.  I believe the question was how to use/extend the ExceptionHandlingProxyBase class to call asnychronous methods without having to set a service reference in Visual Studio.  Morten's suggestion above requires adding a service reference and then checking the "Generate asynchronous operations".

Michele, can you help us with this please?

Regards - Dave