I have a Streaming WCF service. It receives a stream of a serialized class called ContentObjectData
. The bytes I receive in from the stream I have temporarily placed in an ArrayList
as I don't know how big the Stream
is and to be honest I don't know what to do with them anyway.
The ContentObjectData Class:
[Serializable]
public class ContentObjectData
{
string Hash { get; set; }
string Data { get; set; }
string FileName { get; set; }
string DisplayImage { get; set; }
}
This is the Service's Method that receives the stream from the client.
[OperationContract]
public void SendContentObject(Stream data)
{
ArrayList alBytes = new ArrayList();
Console.WriteLine("Client connected");
int count;
byte[] buffer = new byte[4096];
while ((count = data.Read(buffer, 0, buffer.Length)) > 0)
{
alBytes.AddRange(buffer);
}
long i = alBytes.Count;
data.Close();
}
At this moment in time I am sending an Image for testing using the following methods:
private void btnLoadImage_Click(object sender, EventArgs e)
{
DialogResult dr = OFD.ShowDialog();
if (dr == DialogResult.OK)
{
foreach (string filename in OFD.FileNames)
{
try
{
ContentObject co = new ContentObject();
co.Data = LoadFile(filename);
co.Hash = Hashing.HashString(co.Data);
co.DisplayImage = co.Data;
co.FileName = co.Hash;
Stream stream = SerializeToStream(co);
SendContentObject(stream);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
private void SendContentObject(Stream stream)
{
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None, false);
// TimeSpan.MaxValue is interpreted with a rounding error, so use 24 days instead
binding.SendTimeout = TimeSpan.FromDays(24);
binding.TransferMode = TransferMode.Streamed;
ChannelFactory<RaptorStreamingHost> factory = new ChannelFactory<RaptorStreamingHost>(
binding, new EndpointAddress("net.tcp://ccs-labs.com:804/"));
RaptorStreamingHost service = factory.CreateChannel();
service.SendContentObject(stream);
((IClientChannel)service).Close();
}
private string LoadFile(string filename)
{
return Hashing.BytesToString(File.ReadAllBytes(filename));
}
public static Stream SerializeToStream(object objectType)
{
MemoryStream stream = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, objectType);
stream.Position = 0L; // REMEMBER to reset stream or WCF will just send the stream from the end resulting in an empty stream!
return (Stream)stream;
}
I have this to DeSerialize but it doesn't make much sense to me:
public static object DeserializeFromStream(MemoryStream stream)
{
IFormatter formatter = new BinaryFormatter();
stream.Seek(0, SeekOrigin.Begin);
object objectType = formatter.Deserialize(stream);
return objectType;
}
How do I convert the ArrayList of Bytes (I guess DeSerialize them) into a New ContentObject?
Update One Ah So close! Ok in this method
public static ContentObjectData DeserializeFromStream(MemoryStream stream)
{
IFormatter formatter = new BinaryFormatter();
stream.Seek(0, SeekOrigin.Begin);
ContentObjectData objectType = (ContentObjectData)formatter.Deserialize(stream);
return objectType;
}
I have a problem. The Deserializer can not find the ContentObjectData
because it is looking in the client's Namespace for ContentObjectData
and not this Host's namespace.