Tutorial :Npgsql reader type cast error (was: mono does not serialize complex types well)



Question:

Am I right that serializing complex types is not implemented in Mono 2.4.2 yet, or have I made a mistake?

When I call my remote function, I get an error message:

Cannot cast from source type to destination type.     at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke       (System.Runtime.Remoting.Proxies.RealProxy rp, IMessage msg,       System.Exception& exc, System.Object[]& out_args) [0x00000]  

This is the remote function. I got the same result when I use string[] instead. string makes its way through well.

public List<string> GetHist()  {      NpgsqlConnection conn = new NpgsqlConnection(ConnectStr);      conn.Open();        string cmd = "select * from history";      NpgsqlCommand command = new NpgsqlCommand(cmd, conn);      List<string> s = new List<string>();        try      {          NpgsqlDataReader dr = command.ExecuteReader();          if (dr.Read())          {              for (int i = 0; i < dr.FieldCount; i++)                  s.Add(dr.GetString(i));          }          else              s.Add("(hehe)");      }      finally      {          conn.Close();      }      return s;  }  

The caller:

List<string> w = remoteClass.GetHist();  foreach (string s in w)      Console.Write(s + ", ");  Console.WriteLine();  

I found this at http://mono-project.com/FAQ:_Technical :

What about serialization compatibility? Can I serialize an object in Mono and deserialize it in MS.NET or vice versa?

The serialization format implemented in Mono is fully compatible with that of MS.NET. However, having a compatible format is not enough. In order to successfully exchange serialized objects, the corresponding classes need to have the same internal structure (that is, the same public and private fields) in both sides.

If you are serializing your own classes, there is no problem, since you have control over the assemblies and classes being used for serialization.

However, if you are serializing objects from the framework, serialization compatibility is not guaranteed, since the internal structure of those objects may be different. This compatibility is not even guaranteed between different MS.NET versions or Mono versions.

Our policy is to do our best to make the framework classes compatible between Mono and MS.NET, however sometimes this is not possible because the internal implementation is too different. Notice also that when we change a class to make it compatible with MS.NET, we lose compatibility with older versions of Mono.

In summary, if you are designing an application that will run in different environments and platforms which are not under your control, and which need to share serialized objects (either using remoting, plain files, or whatever), you must be careful with what objects you share, and avoid objects from the framework when possible.

(Notice that this only applies to serializers based on the System.Runtime.Serialization framework, and does not apply to the XmlSerializer).

However, it does not even work between mono-2.4.2 and another mono-2.4.2 application.


Solution:1

The problem is in Npgsql so I change the title. These two lines should be equivalent I think, both should return a string but the first one does not work:

dr.GetString(i)  dr[i].ToString()  

To make debugging even harder, .net remoting passed the error to the caller side.


Solution:2

.net serialization does not do generics...yet...grrrrr.

Mono can be tricky but as of .net 2.0, string[] should work. To be sure, try your code on windows.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »