第14课 数据与通信之WCF 概述

第14课 数据与通信之WCF 概述,第1张

概述概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlight 2系列》文章将从Silverlight 2基础知识、数据 概述

Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic,Visual C#,IronRuby,Ironpython,对JsON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlight 2系列》文章将从Silverlight 2基础知识、数据与通信、自定义控件、动画、图形图像等几个方面带您快速进入Silverlight 2开发。

本文将简单介绍在Silverlight 2中如何与WCF进行通信。

简单示例

在本示例中,我们将通过WCF来获取一个最新随笔的列表,在Silverlight中显示出来,最终完后效果如下所示。

先定义一个数据契约:

[DataContract]public class Post{    public Post(int ID,string Title,string author)    {        this.ID = ID;        this.Title = Title;        this.Author = author;    }    [DataMember]    public int ID { get; set; }    [DataMember]    public string Title { get; set; }    [DataMember]    public string Author { get; set; }}

在Web项目中添加一个WCF Service文件,命名为Blog.svc

定义服务契约:

[ServiceContract]public interface IBlog{    [OperationContract]    Post[] Getposts();}

实现服务,这里可以是从数据库或者其他数据源读取,为了演示方便,我们直接初始化一个集合:

public class Blog : IBlog{    public Post[] Getposts()    {        List<Post> posts = new List<Post>()        {            new Post(1,"一步一步学Silverlight 2系列(13):数据与通信之WebRequest","TerryLee"),new Post(2,"一步一步学Silverlight 2系列(12):数据与通信之WebClIEnt",new Post(3,"一步一步学Silverlight 2系列(11):数据绑定",new Post(4,"一步一步学Silverlight 2系列(10):使用用户控件",new Post(5,"一步一步学Silverlight 2系列(9):使用控件模板",new Post(6,"一步一步学Silverlight 2系列(8):使用样式封装控件观感","TerryLee")        };        return posts.ToArray();    }}

修改Web.config中的服务配置,这里使用basic@R_404_6822@Binding绑定,并且开启@R_404_6822@GetEnabled,以便后面我们可以在浏览器中查看服务:

<system.serviceModel>    <behaviors>        <serviceBehaviors>            <behavior name="TerryLee.SilverlightDemo27Web.BlogBehavior">                <serviceMetadata @R_404_6822@GetEnabled="true" />                <serviceDeBUG includeExceptionDetailinFaults="false" />            </behavior>        </serviceBehaviors>    </behaviors>    <services>        <service behaviorConfiguration="TerryLee.SilverlightDemo27Web.BlogBehavior"            name="TerryLee.SilverlightDemo27Web.Blog">            <endpoint address="" binding="basic@R_404_6822@Binding" contract="TerryLee.SilverlightDemo27Web.IBlog">            </endpoint>        </service>    </services></system.serviceModel>

设置一下Web应用程序的端口号为固定端口52424,在浏览器中输入http://localhost:52424/Blog.svc,看看服务是否正常:

好了,现在服务端我们就实现完成了。现在编写界面展示部分,XAML如下:

<GrID Background="#46461F">    <GrID.RowDeFinitions>        <RowDeFinition Height="40"></RowDeFinition>        <RowDeFinition Height="*"></RowDeFinition>    </GrID.RowDeFinitions>    <GrID.ColumnDeFinitions>        <ColumnDeFinition></ColumnDeFinition>    </GrID.ColumnDeFinitions>    <border GrID.Row="0" GrID.Column="0" CornerRadius="15"            WIDth="240" Height="36" Background="Orange"            margin="20 0 0 0" HorizontalAlignment="left">        <TextBlock Text="最新随笔" Foreground="White"                   HorizontalAlignment="left" VerticalAlignment="Center"                   margin="20 0 0 0"></TextBlock>    </border>    <ListBox x:name="posts" GrID.Row="1" margin="40 10 10 10">        <ListBox.ItemTemplate>            <DataTemplate>                <StackPanel OrIEntation="Horizontal">                    <TextBlock Text="{Binding ID}" Height="40" Foreground="Red"></TextBlock>                    <TextBlock Text="{Binding Title}" Height="40"></TextBlock>                    <TextBlock Text="{Binding Author}" Height="40" Foreground="Orange"></TextBlock>                </StackPanel>            </DataTemplate>        </ListBox.ItemTemplate>    </ListBox></GrID>

在Silverlight项目中添加服务引用,输入地址http://localhost:52424/Blog.svc,输入命名空间BlogService。

添加完成后,我们可以在对象浏览器中浏览一下生成的客户端对象:

当然大家也可以手工去编写客户端的代码,请参考WCF的相关内容,这里不再赘述。下面编写调用服务并获取数据,这里仍然是采用异步模式,由于在WCF服务的配置中我们采取了Basic@R_404_6822@Binding,客户端也要采用Basic@R_404_6822@Binding。我们需要注册GetpostsCompleted事件处理方法,以便完成后回调,同时调用GetpostsAsync()方法获取数据。完整的代码如下所示:

public partial class Page : UserControl{    public Page()    {        InitializeComponent();    }    private voID UserControl_Loaded(object sender,RoutedEventArgs e)    {        Binding binding = new Basic@R_404_6822@Binding();        EndpointAddress endPoint = new EndpointAddress(                "@R_404_6822@://localhost:52424/Blog.svc");        BlogClIEnt clIEnt = new BlogClIEnt(binding,endPoint);        clIEnt.GetpostsCompleted += new EventHandler<GetpostsCompletedEventArgs>(clIEnt_GetpostsCompleted);        clIEnt.GetpostsAsync();    }    voID clIEnt_GetpostsCompleted(object sender,GetpostsCompletedEventArgs e)    {        if (e.Error == null)        {            posts.ItemsSource = e.Result;        }    }}

至此,一个完整的在Silverlight 2中调用WCF的示例就完成了,运行后效果如下:

 

结束语

本文简单演示了在Silverlight 2中如何与WCF进行通信,你可以从这里下载示例代码。

总结

以上是内存溢出为你收集整理的第14课 数据与通信之WCF 概述全部内容,希望文章能够帮你解决第14课 数据与通信之WCF 概述所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1044377.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存