Call WCF Service from Silverlight

I have a simple service with one operational contract method Sum

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

When I integrate the web service into a Silverlight application by adding this code to the main page:

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

it does not call the sum method. Moreover, it shows:

obj.sumAsync(int i, int q)
+3
source share
3 answers

Silverlight does not allow the creation of a synchronization proxy for web services. It uses a proxy model of an asynchronous service. There will be two properties for each OperationContract in Silverlight:

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

You should try the following:

private void CallMethod()
{    
    obj.sumAsync(2,2);
    obj.sumAsyncCompleted += (s,e) =>
        {
            if (e.Error == null)
            {
                   MessageBox.Show(e.Result.ToString());
            }
        };
}
+4
source

You marked a method with [OperationContact]. "operational contact" does not make sense.

+1
source

Silverlight works with an asynchronous programming model. Thus, service calls are also asynchronous. You must register the service operation callback before calling the async wcf method:

obj.SumAsyncCompleted += SumAsyncCompleted;
obj.sumAsync(1, 2);

void SumAsyncCompleted(object sender, SumAsyncCompletedEventArgs e)
{
    //do something with e.Result
}
+1
source

All Articles