0

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.

[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.

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. 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.

lbarrous
  • 79
  • 6
  • Possibly related, note that only public fields and properties will be serialized. [Here for private serialization](http://stackoverflow.com/a/802747/314291) (via DataContract serialization) – StuartLC May 31 '15 at 13:57
  • And how can I Serialize the private atributtes of my classes? – lbarrous May 31 '15 at 14:00

2 Answers2

0

You sholud make your members to be seriliazed in Producto class as public members and add Or check this thread to seriliaze private member workaround C# serialize private class member

Community
  • 1
  • 1
User1234
  • 461
  • 3
  • 19
  • I have tried that but still getting the same problem, Should I serialize the 'p' object before using it in the method? – lbarrous May 31 '15 at 15:55
0

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.

Hop it helps for someone!

lbarrous
  • 79
  • 6