views:

162

answers:

4

I am currently trying to send a serialized object over a TCP connection as follows -

BinaryFormatter formatter = new BinaryFormatter();

        formatter.Serialize(clientStream, (Object)Assembly.LoadFrom("test.dll"));

where clientStream is

TcpClient tcpClient = (TcpClient)client;
        NetworkStream clientStream = tcpClient.GetStream();

This is the sending part. But can anyone tell me how do I receive this on the client side (i.e. deserialize it on the other end)?

+2  A: 

You are trying to pass an in-memory representation of the assembly over the wire, not the bytes comprising the assembly file itself. Is that really what you want to do?

Kent Boogaart
Yes. But I am guessing In memory representation is not the entire Assembly (with MSIL). Is this correct?
Pushkar
Yes, that is correct. If you want to transfer the .dll, you must open the file, read it, and send it. And it must not already be loaded by the transferring program.
Randolpho
+1  A: 

Based on the comments, the answer is completely different.

Instead of using a BinaryFormatter, you should get the location of the assembly through the Location property and then use a FileStream to read the bytes of the assembly and send that over the wire.

Serializing the assembly does nothing more than send over the assembly name. You need to send the entire content of the assembly.

casperOne
But I am guessing In memory representation is not the entire Assembly (with MSIL). Is this correct?
Pushkar
@Pushkar: It depends, the in-memory representation of what?
casperOne
of Assembly ofcourse...
Pushkar
@Pushkar: Which begs the question, why do you want to send an assembly over the wire?
casperOne
@Casper - I m trying to implement a Remote Entrusting mechanism which rewuires mobile modules to be sent to client machines. We cannot send COMPLETE mobile EXE hence decided to send code - again cannot send it uncompiled. So had to compile it to DLL and send. Thought it will be better with assembly.
Pushkar
@Pushkar: Changed answer based on the comments.
casperOne
@Casper - thanks. Ill send the entire DLL file now.
Pushkar
A: 

Try using BinaryWriter for writing to stream and BinaryReader for reading from it.

Migol
+3  A: 

Don't serialize the assembly. Send the assembly itself just by loading it as a file and sending those bytes to the other side.

Then, when both sides have the same code, send the object via serialization. I believe the AppDomain which deserializes the object will have to have the relevant assembly loaded (or at least available to be loaded).

Jon Skeet