user1386919
user1386919

Reputation:

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)

Upvotes: 3

Views: 1879

Answers (3)

Shoaib Shaikh
Shoaib Shaikh

Reputation: 4585

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());
            }
        };
}

Upvotes: 4

daryal
daryal

Reputation: 14929

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

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

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

Upvotes: 1

abatishchev
abatishchev

Reputation: 100348

Are you have marked the method with [OperationContact]. "operational contact" makes no sense.

Upvotes: 1

Related Questions