user2729272
user2729272

Reputation: 359

How to convert sync calls to Async call of WCF?

At present we have a couple of WCF service. They are called sysnchronously as follows,

XYZ.XYZClient test= new XYZ.XYZClient();
bool result = test.Add(1,2);

Can someone explain me how we can convert this onto a Async call?

Upvotes: 1

Views: 222

Answers (2)

hutchonoid
hutchonoid

Reputation: 33306

If you follow this MSDN article.

It would become:

double value1 = 100.00D;
double value2 = 15.99D;
XYZ.XYZClient test= new XYZ.XYZClient();
test.AddCompleted += new EventHandler<AddCompletedEventArgs>(AddCallback);
test.AddAsync(1, 2);
Console.WriteLine("Add({0},{1})", value1, value2);

Event args for the operation

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class AddCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
{
    private object[] results;

    public AddCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
            base(exception, cancelled, userState)
    {       this.results = results;         }

    public double Result
    {
        get            {
            base.RaiseExceptionIfNecessary();
            return ((double)(this.results[0]));
        }
    }
}

Code that executes on completion

// Asynchronous callbacks for displaying results.
static void AddCallback(object sender, AddCompletedEventArgs e)
{
    Console.WriteLine("Add Result: {0}", e.Result);
}

Delegate

public event System.EventHandler<AddCompletedEventArgs> AddCompleted;

Upvotes: 2

sisve
sisve

Reputation: 19781

Change your wcf-proxies to generate task-based operations and use async/await.

XYZ.XYZClient test= new XYZ.XYZClient(); 
bool result = await test.AddAsync(1, 2);

Service Reference Settings

Upvotes: 0

Related Questions