简体   繁体   English

远程C#中的序列化异常

[英]Serialization exception in remoting C#

I've been trying a lot before writing this question but I couldn't find any answer for my problem. 在写这个问题之前,我已经做了很多尝试,但是找不到任何答案。

I'm making a remoting program, It's like a store, I've implemented the .dll, the server and the client, and It is everything ok except the fact that I have a big problem related to Serialization, this is a little summary about my code. 我正在制作一个远程程序,就像一个商店,已经实现了.dll,服务器和客户端,一切正常,除了我与序列化有关的大问题之外,这是一个小小的摘要。关于我的代码。

[Serializable]
public class Almacen
{
    int nusuarios_on, nusuarios_reg;
    bool server_on;
    ArrayList clientes_activos;
    ArrayList clientes_registrados;
    ArrayList productos_almacen;}

[Serializable]
public class Cliente
{
    protected int id;
    protected String usuario, dni;
    private String contraseña;
    Pedido pedido;}

[Serializable]
public class Pedido
{
    ArrayList productos;
    bool confirmado;
    int preciototal;}

 [Serializable]
public class Producto
{
    private String nombre;
    private int codigo, precio, unidades, stock; //Stock solo lo usamos en el almacen, y unidades solo en pedido.}

Then, I have my .dll service, in which I have a method with a 'Pedido' object passed by value in parameter, when that method is called. 然后,我有了我的.dll服务,其中有一个方法,当调用该方法时,该方法的'Pedido'对象将通过参数中的值传递。

public class ServicioAlmacen : MarshalByRefObject
{
    private Almacen almacen;
    .
    .
    .
    public int añadirProductoAlmacen(int id, Producto p) {
    if(id==1) {
        if(!productoRepetido(p)) {
            almacen.getProductos_almacen().Add(p);
            Console.WriteLine("Añadido el producto {0} al almacen.", p.getCodigo());
            return 0;
        }
        else {
            Console.WriteLine("Intento de añadir un producto repetido.");
            return -1;
        }
    }
    else {
        Console.WriteLine("Acceso denegado.");
        return -2;
    }   
   }
    .
    .  
    .
}

When this method is called in the client side, I get a SerializationException which tells me that ServiceAlmacen.Producto is NOT tagged as Serialized. 在客户端调用此方法时,我得到一个SerializationException,它告诉我ServiceAlmacen.Producto未标记为Serialized。 I dont understand why I get that exception because I tagged every class as Serialized, any idea? 我不明白为什么会收到该异常,因为我将每个类都标记为序列化,有什么想法吗? Thanks. 谢谢。

PD: If you need my client or server code let me know and i will edit to post. PD:如果您需要我的客户或服务器代码,请告诉我,我将进行编辑以发布。

您应该使您的成员在Producto类中成为公共成员,并添加或检查此线程来对私有成员进行变通解决方法C#序列化私有类成员

I finally got the solution! 我终于找到了解决方案!

Answer: 回答:

You need to add above the declaration of the class "[Serializable]" as it's attached in my code, but you also have to extend the class "MarshalByRefObject" in every class that needs to be Serialized, just extending that every Serialization exception is solved. 您需要在我的代码中附加“ [Serializable]”类的声明,但您还必须在每个需要序列化的类中扩展“ MarshalByRefObject”类,只需扩展每个已解决的序列化异常即可。

Hop it helps for someone! 希望对某人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM