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
ClassHow 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!