WCF学习笔记(5)——wcf序列化List<T>并返回

本文将部署一个wcf+silverlight简单实例,完成wcf返回我们自定义的类型,并以List<T>显示在silverlight上,以下是详细步骤:


 

1.new一个wcf服务,注意要申明一下我们需要返回的自定义类型,[KnownType(typeof(Terminal))](本例自定义的类型为Terminal)

    [DataContract]//实体类契约
    [KnownType(typeof(Terminal))]
    public class Terminal
    {
        [DataMember]
        public object pkCode
        {
            get;
            set;
        }
        [DataMember]
        public object sName
        {
            get;
            set;
        }
        [DataMember]
        public object fkArea
        {
            get;
            set;
        }
        [DataMember]
        public object nState
        {
            get;
            set;
        }
}

对应我们的wcf服务契约也要申明一下

    [ServiceContract]
    [ServiceKnownType(typeof(Terminal))]
    public interface IServiceData
    {
        [OperationContract]
        List<Terminal> GetTerminals();

    }
    public class ServiceData : IServiceData
    {
        public List<Terminal> GetTerminals()
        {
       //获取业务数据,根据你的实际开发环境自行编写  List
<Terminal> list = new List<Terminal>(); DataTable dt = IocMgr.Get<ITerminalMgr>().GetTer(); foreach (DataRow row in dt.Rows) { Terminal ter = new Terminal(); ter.pkCode = row["pkcode"]; ter.sName = row["sname"]; ter.fkArea = row["fkarea"]; ter.nState = row["nstate"]; list.Add(ter); } return list; } }

2.下面来准备一个silverlight程序来作为演示的舞台,并添加我们的wcf服务引用

简单的布局一下我们的silverlight舞台

    <Grid x:Name="LayoutRoot">
        <sdk:DataGrid x:Name="datagrid" AutoGenerateColumns="False" Margin="12" >
            <sdk:DataGrid.Columns >
                <sdk:DataGridTextColumn Header="终端编码" Binding="{Binding pkCode}"/>
                <sdk:DataGridTextColumn Header="点位名称" Binding="{Binding sName}"/>
                <sdk:DataGridTextColumn Header="区域" Binding="{Binding fkArea}"/>
                <sdk:DataGridTextColumn Header="状态" Binding="{Binding nState}"/>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>
    </Grid>

后台cs中,添加调用wcf方法

        // 当用户导航到此页面时执行。本例使用了导航sl程序,根据你的实际开发环境自行修改
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Binding binding = new BasicHttpBinding();
            EndpointAddress endPoint = new EndpointAddress("http://localhost:9090/Service.svc");
            WCFServiceData.ServiceDataClient client = new WCFServiceData.ServiceDataClient(binding, endPoint);
            client.GetTerminalsCompleted += new EventHandler<WCFServiceData.GetTerminalsCompletedEventArgs>(client_GetTerminalsCompleted);
            client.GetTerminalsAsync();
        }

        void client_GetTerminalsCompleted(object sender, WCFServiceData.GetTerminalsCompletedEventArgs e)
        {
            List<WCFServiceData.Terminal> list = e.Result;
            this.datagrid.ItemsSource = list;
        }

编译一下,你会得到如下的异常提示

分析异常,我们发现wcf序列化后返回的类型,并不是我们预想的List<Terminal>,而是Terminal[],

解决方法,需要设置一下vs的默认配置服务引用

修改数据类型中的集合类型,选择System.Collections.Generic.List(默认为System.Array)

ok,重新编译一下我们的项目,F5运行一下,问题解决


 

posted on 2013-01-29 10:50  影天  阅读(7585)  评论(2编辑  收藏  举报

导航