Web Service Complex Type returning
-
Hi, I have this class:
[Serializable]
public class Item
{
public string ProdName { get; set; }
public string Qty { get; set; }
}[Serializable]
public class Order
{
public DateTime Dt;
public int CustomerId;
public List Items;
}It's implemented in a separated assembly and is used in a lot of projects (the class is bigger than it appears here) When I made a webService to return a List, I have two problems. 1) The WebReference I add in the client has a definition of Order that the compiler thinks it's not from the assembly they are defined into. I can't cast. I have to create a List and populate with the data returned from the WebMethod. Worst, It doesn't return a List<>, it returns an []. 2) The Item.Items is not a member of this new (mirror) type the webservice declares? Is there a way to fix it? At least the #2?
Thanks, Dirso
-
Hi, I have this class:
[Serializable]
public class Item
{
public string ProdName { get; set; }
public string Qty { get; set; }
}[Serializable]
public class Order
{
public DateTime Dt;
public int CustomerId;
public List Items;
}It's implemented in a separated assembly and is used in a lot of projects (the class is bigger than it appears here) When I made a webService to return a List, I have two problems. 1) The WebReference I add in the client has a definition of Order that the compiler thinks it's not from the assembly they are defined into. I can't cast. I have to create a List and populate with the data returned from the WebMethod. Worst, It doesn't return a List<>, it returns an []. 2) The Item.Items is not a member of this new (mirror) type the webservice declares? Is there a way to fix it? At least the #2?
Thanks, Dirso
1. As far as I know, XML doesn't have a list concept. It's just plain text. So a List is going to be returned as an array. However in the receiving end you could cast your T[] to List 2. You need XmlRoot and XmlInclude attributes in your WebMethod (for every user defined class - in your case for Order and Item). Refer these posts http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlincludeattribute.aspx[^] A forum discussion[^]
Cheers, Karthik
modified on Sunday, October 24, 2010 11:00 AM
-
Hi, I have this class:
[Serializable]
public class Item
{
public string ProdName { get; set; }
public string Qty { get; set; }
}[Serializable]
public class Order
{
public DateTime Dt;
public int CustomerId;
public List Items;
}It's implemented in a separated assembly and is used in a lot of projects (the class is bigger than it appears here) When I made a webService to return a List, I have two problems. 1) The WebReference I add in the client has a definition of Order that the compiler thinks it's not from the assembly they are defined into. I can't cast. I have to create a List and populate with the data returned from the WebMethod. Worst, It doesn't return a List<>, it returns an []. 2) The Item.Items is not a member of this new (mirror) type the webservice declares? Is there a way to fix it? At least the #2?
Thanks, Dirso
Dirso wrote:
- The WebReference I add in the client has a definition of Order that the compiler thinks it's not from the assembly they are defined into. I can't cast. I have to create a List and populate with the data returned from the WebMethod. Worst, It doesn't return a List<>, it returns an [].
This is correct (and good!) behaviour. The web service is supposed to be agnostic of the client (for example a Java client can consume a .Net web service and vice versa). This is the reason the list is returned as an array, as all languages have the concept of an array, but not a list. The same goes for the
Order
type not casting, the message passed between the client and server is just a "value bag" that looks like an object, you are supposed to populate proper objects from them when then response is recieved. When you generate the client, a new class type is created. The generated class will have a different namespace but normally the same name as the originating server type. This won't cast as it is a completely different type as far as the runtime is concerned, you have to write a convertor if you wish to include the original class. Web Services != Object Orientation. -
1. As far as I know, XML doesn't have a list concept. It's just plain text. So a List is going to be returned as an array. However in the receiving end you could cast your T[] to List 2. You need XmlRoot and XmlInclude attributes in your WebMethod (for every user defined class - in your case for Order and Item). Refer these posts http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlincludeattribute.aspx[^] A forum discussion[^]
Cheers, Karthik
modified on Sunday, October 24, 2010 11:00 AM
-
Dirso wrote:
- The WebReference I add in the client has a definition of Order that the compiler thinks it's not from the assembly they are defined into. I can't cast. I have to create a List and populate with the data returned from the WebMethod. Worst, It doesn't return a List<>, it returns an [].
This is correct (and good!) behaviour. The web service is supposed to be agnostic of the client (for example a Java client can consume a .Net web service and vice versa). This is the reason the list is returned as an array, as all languages have the concept of an array, but not a list. The same goes for the
Order
type not casting, the message passed between the client and server is just a "value bag" that looks like an object, you are supposed to populate proper objects from them when then response is recieved. When you generate the client, a new class type is created. The generated class will have a different namespace but normally the same name as the originating server type. This won't cast as it is a completely different type as far as the runtime is concerned, you have to write a convertor if you wish to include the original class. Web Services != Object Orientation.