Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a WCF service from Silverlight

I have a simple service with a single operational contract method called Sum

[OperationContact]
int sum(int i, int q);

When I am integrating the web service into a Silverlight app, adding this code into the main page:

ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();

it doesn't call sum method. Moreover it shows:

obj.sumAsync(int i, int q)

1 Answers

Silverlight doesn't allow creation of a sync proxy of Web Services. It uses an async service proxy model. There will be two properties for each OperationContract in Silverlight:

obj.sumAsync(int i, int q, object state)
obj.sumAsyncCompleted; // Event

You should try this:

private void CallMethod()
{    
    obj.sumAsync(2,2);
    obj.sumAsyncCompleted += (s,e) =>
        {
            if (e.Error == null)
            {
                   MessageBox.Show(e.Result.ToString());
            }
        };
}
like image 114
Shoaib Shaikh Avatar answered Jan 03 '26 13:01

Shoaib Shaikh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!