代码之家  ›  专栏  ›  技术社区  ›  netmajor

使用WCF服务的区别-控制台与Silverlight

  •  0
  • netmajor  · 技术社区  · 15 年前

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

    实施

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

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

    但在Silverlight中我必须这样:

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

    为什么在SL中我看不到第一个简短的解决方案,而只看到事件处理程序? 或者更好。。。为什么在控制台应用程序中我可以选择同步操作生成,而在SL中我必须使用生成异步操作…:/

    2 回复  |  直到 15 年前
        1
  •  2
  •   Ozan HELPY    15 年前

    同步调用将停止silverlightui线程,也可能停止正在执行的环境,即浏览器。为了防止这种情况,只允许异步调用。

    当然,这在一开始是不寻常的,但从长远来看,将视图和服务层解耦实际上是有帮助的。

        2
  •  1
  •   kojo    15 年前

    Silverlight不支持同步调用(这是您在控制台应用程序中正在执行的操作)。

    更新: http://forums.silverlight.net/forums/p/34531/104526.aspx