Search code examples
c#wcf-client

difference in consume WCF service - Console vs Silverlight


Can someone tell my why when I have wcf contract:

    [ServiceContract]
public interface IService1
{
    [OperationContract]
    string TestGetName();
}

and implementation

 public string TestGetName()
    {
        return "Kasia";
    }

When I try consume it in Console app I can do just that:

    Service1Client client = new Service1Client();
    Console.WriteLine((client.TestGetName()));

but in Silverlight I must use that way :

            Service1Client clientTest = new Service1Client();
            clientTest.TestGetNameCompleted += new EventHandler<TestGetNameCompletedEventArgs>(clientTest_TestGetNameCompleted);
            clientTest.TestGetNameAsync();
    void clientTest_TestGetNameCompleted(object sender, TestGetNameCompletedEventArgs e)
    {
            this.dataGridChild.DataContext = e.Result;

    }

Why in SL I don't see this first short solution, but only this with Event handlers? Or better... why in Console app I can choose synchro operation generation and in SL I must use Generate asynchronous operations... :/


Solution

  • A synchronous call would stop the Silverlight UI thread and possibly the executing environment, i.e. the browser. To prevent this, only asynchronous calls are allowed.

    Of course this is something unusual at first, but in the long run it is actually helpful to decouple the view and service layer.