Generic List get converted into an Array when sent through Web Service?

Last post 09-19-2005 12:42 AM by JDHill. 4 replies.

Sort Posts:

  • Generic List get converted into an Array when sent through Web Service?

    09-18-2005, 9:21 AM
    • Participant
      1,500 point Participant
    • badbanana
    • Member since 04-28-2004, 8:21 AM
    • Posts 300

    Hello, i have a customer object like this:

    public
    class Customer
    {
    public Customer()
    {

    }

    private string customername;
    public string CustomerName
    {
    get { return customername; }
    set { customername = value; }
    }

    }


    And a generic Customer collection that can contain customer objects like this:

    public class CustomerList : List<Customer>
    {
    public CustomerList()
    {

    }

    }


    I populate a CustomerList with some Customer objects and send the CustomerList through a web service:

    [WebMethod]
    public CustomerList GetCustomerList()
    {

    CustomerList myCustomerList = new CustomerList();
    Customer myCustomer = new Customer();
    myCustomer.CustomerName =
    "Hello this is a test";
    myCustomerList.Add(myCustomer);

    return myCustomerList;
    }


    But  on the client end, the windows application that is going to consume the web service, this generic CustomerList has turned into an
    Array of Customers?? Customer[] Customers=new Customers[]... Why is that, what am i doing wrong?, i want to the CustomerList to "stay" a CustomerList even though im sending it through a web service, but it gets converted into an array Sad [:(] , Im using the August CTP..

    And if i invoke this WebMethod the XML look like this:


    <?xml version="1.0" encoding="utf-8" ?>

    - <ArrayOfCustomer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
    - <Customer>
      <CustomerName>Hello this is a test</CustomerName>
      </Customer>
      </ArrayOfCustomer>


    As you can see, the CustomerList has turned into an Array of Customers?..






    .
  • Re: Generic List get converted into an Array when sent through Web Service?

    09-18-2005, 5:35 PM
    • Member
      460 point Member
    • JDHill
    • Member since 09-14-2005, 5:59 PM
    • Oak Grove, Minnesota
    • Posts 92
    SoapFormatter does not support Generics.

    I've not done this, but since the app on the other end is .Net you should be able to serialize with the BinaryFormatter, read the FileStream into a String, pass the String over the wire, read it back into a Stream, and deserialize into your List<Customer> with no firewall troubles.

    Hope this is helpful.
    "Make everything as simple as possible, but not simpler." ~ Albert Einstein
  • Sv: Re: Generic List get converted into an Array when sent through Web Service?

    09-18-2005, 5:43 PM
    • Participant
      1,500 point Participant
    • badbanana
    • Member since 04-28-2004, 8:21 AM
    • Posts 300
    Thanks. But ah man, that was some hefty info Cool [H].
    Would you be so kind showing me some code how to accomplish this?. Big Smile [:D]
  • Re: Sv: Re: Generic List get converted into an Array when sent through Web Service?

    09-19-2005, 12:42 AM
    • Member
      460 point Member
    • JDHill
    • Member since 09-14-2005, 5:59 PM
    • Oak Grove, Minnesota
    • Posts 92
    Here you go:

    What we're doing is using a BinaryFormatter to put the CustomerList in a temp file through a FileStream.  Then we're reading the bytes out of that file into a Byte Array and pushing them over the wire.  On the other end, we read them into a file, and then read that file into a FileStream for the BinaryFormatter to Deserialize.

    The transmission is done in base64binary, which, while being a text-representation, is not messed up by the xml UTF-8 encoding.  I don't work with Streams too much, and I didn't have the time to figure out how to get rid of the intermediate files by using another type of Stream.

    (I'm a VB.Net guy, so you'll have to translate for yourself)


    service.asmx


    Imports System.Web

    Imports System.Web.Services

    Imports System.Web.Services.Protocols

    Imports System.IO

    Imports System.Runtime.Serialization

    Imports System.Runtime.Serialization.Formatters.Binary

     

    <WebService(Namespace := "http://tempuri.org/")> _

    <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _

    Public Class Service

    Inherits System.Web.Services.WebService

    Public Sub Service

    End Sub

    Private cl As New CustomerList

    <WebMethod()> _

    Public Function GetCustomerList() As Byte()

    Dim c As New Customer

    c.CustomerName = "Customer1"

    cl.Add(c)

    Dim c2 As New Customer

    c2.CustomerName = "Customer2"

    cl.Add(c2)

    Dim c3 As New Customer

    c3.CustomerName = "Customer3"

    cl.Add(c3)



    Dim
    s As New FileStream(Server.MapPath("temp.str"), FileMode.Create)

    Dim b As New BinaryFormatter

    b.Serialize(s, cl)

    s.Close()

    Dim bstr As Byte() = (File.ReadAllBytes(Server.MapPath("temp.str")))

    File.Delete(Server.MapPath("temp.str"))

    Return bstr

    End Function

    End Class



    <Serializable()> _

    Public Class Customer

    Private cn As String

    Public Property CustomerName() As String

    Get

    Return cn

    End Get

    Set(ByVal value As String)

    cn = value

    End Set

    End Property

    End Class



    <Serializable()> _

    Public Class CustomerList

    Inherits System.Collections.Generic.List(Of Customer)

    End Class






    How the transmission looks at this point is:

    <?xml version="1.0" encoding="utf-8" ?>
      <base64Binary xmlns="http://tempuri.org/">AAEAAAD/////AQAA
    AAAAAAAMAgAAAD9BcHBfQ29kZSwgVmVyc2lvbj0wLjAu
    MC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRv
    a2VuPW51bGwFAQAAAAxDdXN0b21lckxpc3QDAAAADUxpc
    3RgMStfaXRlbXMMTGlzdGAxK19zaXplD0xpc3RgMStfdmVyc
    2lvbgQAAApDdXN0b21lcltdAgAAAAgIAgAAAAkDAAAAAwA
    AAAMAAAAHAwAAAAABAAAABAAAAAQIQ3VzdG9tZXICA
    AAACQQAAAAJBQAAAAkGAAAACgUEAAAACEN1c3RvbW
    VyAQAAAAJjbgECAAAABgcAAAAJQ3VzdG9tZXIxAQUAAA
    AEAAAABggAAAAJQ3VzdG9tZXIyAQYAAAAEAAAABgkAAA
    AJQ3VzdG9tZXIzCw==
    </base64Binary>





    Form1.vb

    this project needs a reference to the .dll that defines CustomerList - add a copy of the .dll to your project and a reference to the .dll



    Imports System.IO

    Imports CustomerList

    Imports System.Runtime.Serialization.Formatters.Binary

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    File.WriteAllBytes("c:\temp.str", My.WebServices.Service.GetCustomerList)

    Dim fs As New FileStream("c:\temp.str", FileMode.Open)

    Dim bf As New BinaryFormatter

    Dim cl As CustomerList = bf.Deserialize(fs)

    fs.Close()

    File.Delete("c:\temp.str")

    For i As Integer = 0 To cl.Count - 1

    Me.TextBox1.Text = Me.TextBox1.Text & cl(i).CustomerName & vbCrLf

    Next

    End Sub

    End Class





    So there you go - Generics over a Web Service without SOAP.

    Have fun!
    "Make everything as simple as possible, but not simpler." ~ Albert Einstein
  • Sv: Re: Sv: Re: Generic List get converted into an Array when sent through Web Service?

    09-19-2005, 5:50 AM
    • Participant
      1,500 point Participant
    • badbanana
    • Member since 04-28-2004, 8:21 AM
    • Posts 300
    Cool Cool [H]
Page 1 of 1 (5 items)