Windows Communication Foundation (WCF) based File Upload service.
Messages
In normal cases you shouldn't have to touch the messages. But in some cases
it becomes necessary. For example, assuming that your service will provide a
File Upload operation. Here is the possible contract interface:
[OperationContract]
void Upload (string fileName, Stream stream);
Unfortunetly, when using Streams, our operation can not take other
parameters. So we cannot tell the fileName to our operation when uploading a
file. You may always create two operations, one for setting the fileName,
and the other one for uploading the file, but this is not a good solution.
In order to solve this problem we will need to create a new message.
[MessageContract]
public class FileMessage
{
[MessageHeader]
public string fileName;
[MessageBody]
public Stream stream;
}
Our message class needs to have the MessageContract attribute. Since WCF
only allows the Stream in message body, we are keeping the fileName in
message headers. To keep a data in message header we are using the
MessageHeader attribute, and to keep a data in message body we are using the
MessageBody attribute.
[OperationContract]
void Upload (FileMessage message);
This is the modified version of our contract.
If you found this post helpful, please "Kick" it so others can find it too: