Error passing List as a parameter to web service
-
Error passing List as a parameter to web service This is my second post on the same subject, in an attempt to solve the same problem. I hope this time I've supplied enough and correct information. Given a basic service, it is possible to return a List from the web service using the GetList() method but the compiler gives an error when trying to send the list as a parameter in the SendList() method. Please check the code bellow. I've revised it. The error is on line # 88 (The compiler error description is at the end of the source code) Note: I've configured the ServiceReference to return System.Collections.Generic.List although I've tested all possible combinations. 1 namespace SilverlightApplication1.Web 2 { 3 [ServiceContract(Namespace = "")] 4 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 5 public class Service1 6 { 7 [OperationContract] 8 public List GetList() 9 { 10 // Add your operation implementation here 11 List myList = new List(); 12 myList.Add(new People() { name = "AAA", age = 42 }); 13 return myList; 14 } 15 [OperationContract] 16 public string SendList(List myList) 17 { 18 return myList[0].name; 19 } 20 } 21 22 public class People 23 { 24 public string name; 25 public int age; 26 } 27 } 28 29 30 "SilverlightApplication1.Home" 31 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 32 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 33 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 34 xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 35 mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" 36 Title="Home" 37 Style="{StaticResource PageStyle}"> 38 39 46
-
Error passing List as a parameter to web service This is my second post on the same subject, in an attempt to solve the same problem. I hope this time I've supplied enough and correct information. Given a basic service, it is possible to return a List from the web service using the GetList() method but the compiler gives an error when trying to send the list as a parameter in the SendList() method. Please check the code bellow. I've revised it. The error is on line # 88 (The compiler error description is at the end of the source code) Note: I've configured the ServiceReference to return System.Collections.Generic.List although I've tested all possible combinations. 1 namespace SilverlightApplication1.Web 2 { 3 [ServiceContract(Namespace = "")] 4 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 5 public class Service1 6 { 7 [OperationContract] 8 public List GetList() 9 { 10 // Add your operation implementation here 11 List myList = new List(); 12 myList.Add(new People() { name = "AAA", age = 42 }); 13 return myList; 14 } 15 [OperationContract] 16 public string SendList(List myList) 17 { 18 return myList[0].name; 19 } 20 } 21 22 public class People 23 { 24 public string name; 25 public int age; 26 } 27 } 28 29 30 "SilverlightApplication1.Home" 31 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 32 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 33 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 34 xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 35 mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" 36 Title="Home" 37 Style="{StaticResource PageStyle}"> 38 39 46
I think the problem is that the
People
objects that you are trying to send are defined on the client side. The service is expecting thePeople
objects that are defined on the service side - they are not the same thing. Try sending a list ofServiceReference1.People
instead ofSilverlightApplication1.People
and see if you get the same error. -
Error passing List as a parameter to web service This is my second post on the same subject, in an attempt to solve the same problem. I hope this time I've supplied enough and correct information. Given a basic service, it is possible to return a List from the web service using the GetList() method but the compiler gives an error when trying to send the list as a parameter in the SendList() method. Please check the code bellow. I've revised it. The error is on line # 88 (The compiler error description is at the end of the source code) Note: I've configured the ServiceReference to return System.Collections.Generic.List although I've tested all possible combinations. 1 namespace SilverlightApplication1.Web 2 { 3 [ServiceContract(Namespace = "")] 4 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 5 public class Service1 6 { 7 [OperationContract] 8 public List GetList() 9 { 10 // Add your operation implementation here 11 List myList = new List(); 12 myList.Add(new People() { name = "AAA", age = 42 }); 13 return myList; 14 } 15 [OperationContract] 16 public string SendList(List myList) 17 { 18 return myList[0].name; 19 } 20 } 21 22 public class People 23 { 24 public string name; 25 public int age; 26 } 27 } 28 29 30 "SilverlightApplication1.Home" 31 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 32 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 33 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 34 xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 35 mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" 36 Title="Home" 37 Style="{StaticResource PageStyle}"> 38 39 46
In addition to Nigel's reply... Since you're using the automagic proxy generator, then you can look at the generated code on the client side: 1) Click on the client project in the Solution Explorer 2) Click the "Show All Files" button on the Solution Explorer's toolbar 3) Drill down the files tree from the service reference to find the Reference.cs file - that will have the generated client-side classes In the generated code, there should be the People class you should be using. If it's not there, try using the DataContractAttribute on the server side:
[DataContract]
public class People
{
[DataMember]
public string name;
[DataMember]
public int age;
}(don't forget to update the service reference on the client to get new generated code!)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
In addition to Nigel's reply... Since you're using the automagic proxy generator, then you can look at the generated code on the client side: 1) Click on the client project in the Solution Explorer 2) Click the "Show All Files" button on the Solution Explorer's toolbar 3) Drill down the files tree from the service reference to find the Reference.cs file - that will have the generated client-side classes In the generated code, there should be the People class you should be using. If it's not there, try using the DataContractAttribute on the server side:
[DataContract]
public class People
{
[DataMember]
public string name;
[DataMember]
public int age;
}(don't forget to update the service reference on the client to get new generated code!)
Mark Salsbery Microsoft MVP - Visual C++ :java:
Mark, It is there. Thanks for the extra info (Being able to look at the generated client-side class is really usefull) The problem is solved. Nigel's post was right. I apologize that I didn't mentioned it on the previous thread. But, there are 2 things I learned, besides these ones. 1. Starting with Net 3.51 it is not necessary to add the [Data Contract] anymore for know types. 2. It appears that SL 3 web services do not require to implement the interface