0

I have a simple registration form, that gets a string username, and a string password. Then, I create a simple instance of a class, where I save both those fields, it's called UserRegisterData, and looks like this:

[Serializable]
public class UserRegisterData
{
    public string userName { get; set; }
    public string userPassword { get; set; }
}

Then, I try to send this class from my client to the server. Before sending, I encode it, using this method:

        public static byte[] ObjectToByteArray(UserRegisterData obj)
        {
            BinaryFormatter bf = new BinaryFormatter();
            using (var ms = new MemoryStream())
            {
                bf.Serialize(ms, obj);
                return ms.ToArray();
            }
        }

Here is my method for decoding the object:

    public static object ByteArrayToObject(byte[] arrBytes)
    {

        BinaryFormatter formatter = new BinaryFormatter();
        MemoryStream ms = new MemoryStream(arrBytes);
        return formatter.Deserialize(ms);

    }

Then, I send it to the server, and it receives the data. However, when I try to decode that object on the sever using the method ByteArrayToObject(byte[] arrBytes), it throws an exception, and doesn't decode the data. here is the exception:

System.Runtime.Serialization.SerializationException: „Unable to find assembly 'LogInForm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.”

I think the problem might be in the fact, that I am serializing and deserializing this object in different projects. Does anyone have a solution?

  • Send the data serialized as XML or JSON. – Jimi Jun 03 '20 at 23:34
  • @Jimi I'm trying to keep it simple so is there any way to pass the data raw, as an encoded object? – Nazar Prokudin Jun 03 '20 at 23:36
  • You are missing the LogInForm assembly in the application where you are deserializing this. Make sure both applications are referencing the same DLL. – Gabriel Luci Jun 03 '20 at 23:40
  • Is there any simple way to reference that application in Visual Studio? I can't see the LoginForm.dll in the files of the project. – Nazar Prokudin Jun 03 '20 at 23:50
  • The keep it simple, serialize the data as XML or JSON. Using any of these, it's 1 to 3 commands to serialize and 1-3 commands to deserialize. Zero hassles and zero need to have binary compatibility. – Jimi Jun 04 '20 at 00:48
  • @NazarProkudin - You just copy the assembly to the server. Is that what you mean? – Enigmativity Jun 04 '20 at 01:21

0 Answers0