Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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;
            }
        }  

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.
Posted

1 solution

For Error 1:

Replace

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


With

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



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 ...:)
 
Share this answer
 
Comments
Purple Cucumber 16-Jun-14 10:55am    
Thank you, it helped. One more thing, I have the following in the service interface:
[OperationContract]
static IEnumerable<worker> GetStuffList();
And class "worker" is unavailable, because it defined in the service1.svc. What can I do about it?
ashok rathod 17-Jun-14 1:25am    
Simplest solution i would suggest is you can one shared assembly and put worker class in that and can add reference of it to service project and your application project

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900