WCF RIA Services 简单应用

时间:2022-01-02 03:14:00

DomainService1:

namespace SilverlightApplication3.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;


    // TODO: 创建包含应用程序逻辑的方法。
    [EnableClientAccess()]
    public class DomainService1 : DomainService
    {
        [Invoke]
        public string getName()
        {
            return " Jack";
        }


        [Invoke]
        public Person getPerson()
        {
            Person p = new Person();

            p.Name = "jack";
            p.Sex = "male";
            return p;
        }

        [Invoke]
        public List<Person> getPersonList()
        {
            List<Person> list = new List<Person>();
            for (int i = 0; i < 10; i++)
            {
                Person p = new Person();
                p.Name = "jack" + i;
                p.Sex = "male" + i;
                list.Add(p);
            }

            return list;
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public string Sex { get; set; }
    }
}

MainPage:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightApplication3.Web;

namespace SilverlightApplication3
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            gert();
        }


        public void gert()
        {
            DomainService1 ds = new DomainService1();
            ds.getName(o => gethhh(o.Value), null);

            ds.getPerson(o => gethhu(o.Value), null);

            ds.getPersonList(o => getlist(o.Value), null);
        }

        void gethhh(object v)
        {
            //MessageBox.Show((string)v);
        }

        void gethhu(object v)
        {
            //Person mm = (Person) v;
            //MessageBox.Show(mm.Name);
        }

        void getlist(object l)
        {
            List<Person> kl = (List<Person>)l;
            MessageBox.Show(kl.FirstOrDefault().Name);
        }
    }
}

附上源码:下载