Tuesday, October 05, 2010

 

Abstract Async calls

Decoupling is important, but how can asynchronous calls be decoupled? Generally making async calls is simple and logic free. However, if there is a large amount of logic in either hooking up, cleaning up, or somewhere in the middle of an asynchronous call it's a good time to do some abstraction.

I found some information about this on: http://csharperimage.jeremylikness.com/2009/07/abstracting-wcf-service-calls-in.html. I simplified this as Silverlight 4's new relative addressing makes things quite a bit easier.

I followed Jeremy's example, creating a silverlight friendly WCF service and adding a multiply service in the svc.cs file:

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CalculatorService
{
[OperationContract]
long Multiply(int x, int y)
{
return x*y;
}
}


Now create a class inside your Silverlight app that handles the async to, in this case, a web service. You could add in additional logic or additional async calls. Perhaps async calls to take a webcam picture or to execute a thread. After all of this logic is completed a single event is fired, CustomServiceCompleted.

public class CalculatorService3
{
//initialize and hold reference to the service client
private CalculatorServiceClient client;
public CalculatorServiceClient Client
{
get {
if (client == null)
client = new CalculatorServiceClient();
return client;
}
set { client = value; }
}

//event raised logic extracted to this class is completed.
public event EventHandler<MultiplyCompletedEventArgs> CustomServiceCompleted;

public CalculatorService3()
{
//ensure the completion method is only registered once
Client.MultiplyCompleted += new EventHandler<MultiplyCompletedEventArgs>(CalculatorService_MultiplyCompleted);
}

void CalculatorService_MultiplyCompleted(object sender, MultiplyCompletedEventArgs e)
{
if (CustomServiceCompleted != null)
{
//this is where you'd modify the e.Result value before returning by creating a new set of Args
MultiplyCompletedEventArgs finalArgs = new MultiplyCompletedEventArgs(new object[] { e.Result }, e.Error, e.Cancelled, e.UserState);
CustomServiceCompleted(this, finalArgs);
}
}

public void Multiply(int x, int y)
{
Client.MultiplyAsync(x, y);
}
}


Calling the webservice through the client side class can be done as follows:

private void Button_Click(object sender, RoutedEventArgs e)
{
Service.CalculatorService3 proxyCalc = new Service.CalculatorService3();
proxyCalc.CustomServiceCompleted += new EventHandler<CalculatorService.MultiplyCompletedEventArgs>(proxyCalc_CustomServiceCompleted);
proxyCalc.Multiply(7, 3);
}

void proxyCalc_CustomServiceCompleted(object sender, CalculatorService.MultiplyCompletedEventArgs e)
{
if (e.Error == null)
MessageBox.Show("result is: " + e.Result.ToString());
}

Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]