Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
I am creating a web application in vb.net. I want to send more than 200 records from server to client, so I have stored all the records in the dataset object and sending that object through socket. Now I know that I should serialize the object & send it through socket, but I am facing problem.
Here is my code:
VB
sql = "Select Transaction_ID,Log_Date,Log_Time,Client_Action,Remarks from ClientLogs  where Account_No=" & ClientAccountNumber & ""
                          dataadapter = New OleDbDataAdapter(sql, connection)
                          dataadapter.Fill(dataset, "ClientLogs")
                          connection.Close()
                          dataadapter.Dispose()
                         
                          Dim send_to_client As Byte()
                          send_to_client = Encoding.ASCII.GetBytes(dataset & Chr(13))
                          networkStream.Write(send_to_client, 0, send_to_client.Length)
                          networkStream.Flush()


I have very little idea about binary serialization(I don't want xml serialization).
I tried to collect some info about <serializable()> but I didn't get much.Is there any way to send the data set object through socket using serializable class and deserialize the dataset object oat the server side.
In my project, database is at server side .

Any constructive suggestions are welcome.
Thanking you in advance
Posted
Updated 16-Jan-18 13:55pm
v2
Comments
Simon_Whale 9-May-11 5:39am    
what are you using to read the byte array?

1 solution

If you're going to use binary serialization, the big problem you'll run into when sending serialized objects to another application is that objects serialized using binary formatter record not only the class name, but the namespace and the assembly name as well. So even if you successfully serialize and then transfer an object via tcp, when you attempt to deserialize it in the remote application, it will fail.

The way to deal with this is to use SerializationBinder.BindToType.

Using BindToType, you can peek into the object before it is deserialized, and instruct BinaryFormatter to get the target Object Type from current assembly.

I have a TCP library here on codeproject that allows you to send serialized objects between client and server, and also between clients connected to your server. The code is available in the download for the article:

https://www.codeproject.com/Articles/1192106/Abstracting-TCP-Communications-and-adding-what-sho

I would download the library and have a look - it should do what you need. If you're still interested in doing it yourself, here's a look at how I set up BindToType (in c#):

C#
public class TypeConvertor : System.Runtime.Serialization.SerializationBinder
            {
                private Assembly targetAssembly;
                public TypeConvertor(Assembly targetAssembly)
                {
                    this.targetAssembly = targetAssembly;
                }

                public override Type BindToType(string assemblyName, string typeName)
                {
                    Type returntype = null;
                    assemblyName = targetAssembly.FullName;
                    Assembly.Load(assemblyName);

                    returntype = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName), false, true);

                    // In VB.NET, the typeName may contain the default namespace also. So our if our remote application
                    // is a VB.NET application, we need to remove that:
                    if (returntype == null && typeName.Contains("."))
                    {
                        // Remove the default namespace here:
                        String vbAssemName = Regex.Split(typeName, @"\.")[0] + ".";
                        typeName = typeName.Replace(vbAssemName, "");

                        // And attempt to get the returntype (our targetAssembly may be a C# application)
                        returntype = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName), false, true);

                        // If targetAssembly is a VB.NET application, we need to get the current default namespace:
                        if (returntype == null)
                        {
                            try
                            {
                                // All the types in our target assembly should be the same. We only need one:
                                Type[] types = targetAssembly.GetTypes();
                                if (types.Length > 0)
                                {
                                    // And include it in our typename:
                                    returntype = Type.GetType(String.Format("{0}, {1}", Regex.Split(types[0].FullName, @"\.")[0] + "." + typeName, assemblyName), false, true);
                                }
                            }
                            catch (Exception) { }
                        }
                    }

                    return returntype;
                }
            }


The code you will want to look at to see an example of serializing and deserializing objects in that project can be found in Core.cs between lines 399 and 465.

- Pete
 
Share this answer
 
v2
Comments
Ralf Meier 17-Jan-18 3:10am    
Did you realize that this question ist nearly 7 years old ?
Do you think that this is still an actual problem ...?
pdoxtader 17-Jan-18 7:33am    
I didnt notice the date of creation... Just that it had been updated recently. I think it is.

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