2

我有一个客户端/服务器项目,我正在尝试通过套接字将 DataTable(从 TableAdapter 中提取)从服务器发送到客户端。我的服务器命名空间是 srvCentral,我的客户端是 appClient。当我尝试在客户端反序列化 DataTable 时,它​​会抛出一个序列化异常,提示无法找到程序集 'srvCentral,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null' svchost 挂起并强制我关闭,并使用这样的活页夹:

sealed class AllowAllAssemblyVersionsDeserializationBinder : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        Type typeToDeserialize = null;

        String currentAssembly = Assembly.GetExecutingAssembly().FullName;

        // In this case we are always using the current assembly
        assemblyName = currentAssembly;

        // Get the type using the typeName and assemblyName
        typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
                                         typeName, assemblyName));

        return typeToDeserialize;
    }
}

异常仍然存在......难道不是假设 DataTable 在任何地方都可以反序列化吗?我究竟做错了什么?

我的序列化代码是:

public byte[] Serializar(Object item)
{
    BinaryFormatter formatter = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    formatter.Serialize(ms, item);
    return ms.ToArray();
}

public Object Deserializar(byte[] buffer)
{
    BinaryFormatter formatter = new BinaryFormatter();
    MemoryStream ms = new MemoryStream(buffer);

    formatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;

    formatter.Binder = new AllowAllAssemblyVersionsDeserializationBinder();

    Object a = formatter.Deserialize(ms);

    return a;
}

sealed class AllowAllAssemblyVersionsDeserializationBinder : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        Type typeToDeserialize = null;

        String currentAssembly = Assembly.GetExecutingAssembly().FullName;

        // In this case we are always using the current assembly
        assemblyName = currentAssembly;

        // Get the type using the typeName and assemblyName
        typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
                                         typeName, assemblyName));

        return typeToDeserialize;
    }
}

经过一番挖掘,我已经解决了这个问题;

这不是解决问题的更好方法,而是避免问题的简单方法。对于小事来说就足够了。如果您只想从表中检索数据以显示内容,请使用此选项。

namespace YourLibrary
{
[Serializable]
public class Tabela: ISerializable
{
    protected ArrayList colNames;
    protected ArrayList colTypes;
    protected ArrayList dataRows;

    public Tabela()
    {

    }

    public Tabela (DataTable dt)
    {
        colNames = new ArrayList();
        colTypes = new ArrayList();
        dataRows = new ArrayList();
        // Insert column information (names and types)
        foreach(DataColumn col in dt.Columns)
        {
            colNames.Add(col.ColumnName); 
            colTypes.Add(col.DataType.FullName);   
        }

        // Insert rows information
        foreach(DataRow row in dt.Rows)
            dataRows.Add(row.ItemArray);
    }

    public Tabela(SerializationInfo info, StreamingContext context)
    {
        colNames = (ArrayList)info.GetValue("colNames",typeof(ArrayList));
        colTypes = (ArrayList)info.GetValue("colTypes",typeof(ArrayList));
        dataRows = (ArrayList)info.GetValue("dataRows",typeof(ArrayList));

    }

    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("colNames", colNames);
        info.AddValue("colTypes", colTypes);
        info.AddValue("dataRows", dataRows);
    }

    public DataTable GenerateDataTable()
    {
        DataTable dt = new DataTable();

        // Add columns
        for(int i=0; i<colNames.Count; i++)
        {
            DataColumn col = new DataColumn(colNames[i].ToString(), 
                Type.GetType(colTypes[i].ToString() ));     
            dt.Columns.Add(col);
        }

        // Add rows
        for(int i=0; i<dataRows.Count; i++)
        {
            DataRow row = dt.NewRow();
            row.ItemArray = (object[]) dataRows[i];
            dt.Rows.Add(row);
        }

        dt.AcceptChanges();
        return dt;
    }
}
}
4

1 回答 1

13

我究竟做错了什么?

叹; 1:使用DataTable和 2:使用BinaryFormatter

让我们先看后者;BinaryFormatter是一个以类型为中心的序列化器。实际上,如果您只是使用DataTable而不是类型化 DataTable的子类,那么您可能会侥幸逃脱,但BinaryFormtter最终希望每一端都具有完全相同的类型。而你没有那个。即使你这样做了,每次你对管道的一端进行版本控制时,事情都会变得有点……狡猾(除非你在这方面投入额外的精力)。

作为临时修复,对于这一步,只需使用DataTable而不是类型化的DataTable子类,它可能会起作用。

然而,扔掉DataTable也是一件非常尴尬的事情 - 相当笨拙且用途如此普遍。如果您需要它提供的内容(特别是动态列),那么 ....可能会一推,但在大多数情况下,使用基本的 POCO/DTO 模型会更可取。这也更容易在客户端/服务器边界上表达,包括大多数 IPC 工具。例如,下面的 POCO/DTO 类(或它们的列表)非常友好:

public class Order {
    public int OrderID {get;set;}
    public string Reference {get;set;}
    ...
}

就个人而言,我强烈建议您考虑切换到更简单的基于类的模型,使用对特定类型不挑剔的序列化程序;XmlSerializerJavascriptSerializer工作良好。如果您需要小/高效的数据,那么 protobuf-net 也值得一看。所有这些都在套接字上工作得很好,也是。

于 2012-05-24T10:20:42.047 回答