如何使用WCF服务填充长列表 [英] How to populate longlist using WCF service

查看:49
本文介绍了如何使用WCF服务填充长列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过WCF服务在我的WP 8页面上填充长列表。我正在关注本教程但得到错误。这是我的服务实现:

I want to populate longlist on my WP 8 page via WCF service. I'm following this tutorial but get errors. Here is my service implementation:

private static IEnumerable<worker> GetStuffList()  {
            List<worker> stuffList = new List<worker>();
 stuffList.Add(new worker() { Name = "John", Age = 23, Sex = true }); //error 1
            return stuffList;     }
        private List<Group<worker>> GetStuffList() // error 2
        {            IEnumerable<worker> stuffList = GetStuffList(); // error 3
            return GetItemGroups(stuffList, c => c.Age.ToString());  }

        private static List<Group<T>> GetItemGroups<T>(IEnumerable<T> itemList, Func<T, string> getKeyFunc)
        {
            IEnumerable<Group<T>> groupList = from item in itemList
                                              group item by getKeyFunc(item) into g
                                              orderby g.Key
                                              select new Group<T>(g.Key, g);
            return groupList.ToList();
        }
        public class Group<T> : List<T>
        {
            public Group(string name, IEnumerable<T> items)
                : base(items)
            {
                this.Title = name;
            }
            public string Title
            {
                get;
                set;
            }   }
        public class worker
        {
            public string Name;
            public int Age;
            public bool Sex;
            public worker(string Name, int Age, bool Sex)
            {
                this.Age = Age;
                this.Name = Name;
                this.Sex = Sex;
            }
        }  



错误:

1)'WcfService1.Service1.worker'不包含构造函数取0个参数。

2)输入'WcfService1.Service1';已经定义了一个名为'GetStuffList'的成员,它具有相同的参数类型。

3)以下方法或属性之间的调用是不明确的:'​​WcfService1.Service1.GetStuffList()'和'WcfService1.Service1。 GetStuffList()'



请你更正我的代码。


Errors:
1) 'WcfService1.Service1.worker' does not contain a constructor that takes 0 arguments.
2) Type 'WcfService1.Service1'; already defines a member called 'GetStuffList' with the same parameter types.
3) The call is ambiguous between the following methods or properties: 'WcfService1.Service1.GetStuffList()' and 'WcfService1.Service1.GetStuffList()'

Could you correct my code please.

推荐答案

错误1 :



替换



For Error 1:

Replace

List<worker> stuffList = new List<worker>();
stuffList.Add(new worker() { Name = "John", Age = 23, Sex = true }); //error 1











With

List<worker> stuffList = new List<worker>();
stuffList.Add(new worker("John",23,true)); //error 1







错误2:

因为你创建了GetStuffList方法参数相同所以你不能在WCF服务的情况下这样做,就像在WCF中一样服务方法不能像c#那样重载。

你必须使用WCF的Name属性OperationContract,或者您可以使用不同的名称制作两个方法(例如GetStuffList,GetStuffEnumerable 按照返回类型)并根据您的要求在您的代码中调用







对于错误3:

如果您遵循错误2的解决方案。我认为错误3也将得到解决







快乐编码...:)




For Error 2:
As you have created GetStuffList Method Parameter Same so u cannot do this in case of WCF Service as in WCF Service Method Overloading is not possible as c#.
You have to use Name Property of WCF OperationContract for that or you can make two methods with different name (e.g GetStuffList,GetStuffEnumerable as per return type) and call as per your requirement in your code



For Error 3:
If you follow solution of error 2. I think Error 3 will also get it resolved



Happy Coding ...:)


这篇关于如何使用WCF服务填充长列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆