Binary data in WCF
Binary data in WCF
?? WCF automates mapping bytes to base64Binary in XSD
?? Encoding determines what actually hits the wire
[DataContract(Name="photo")]
public class Photo {
[DataMember(Order=1)]
PhotoMetadata metadata;
[DataMember(Order=2)]
byte[] data;
}
[ServiceContract]
public interface IPhotoShare {
[OperationContract]
Photo GetPhoto(string id);
}
Techniques for handling large data
?? WCF provides techniques/hooks for handling large data
• ?? Transport compression
• ?? Streaming
• ?? Chunking
?? Each requires some work and may affect interoperability
Transport compression
?? Consider transport compression when you're forced to use the
text encoding for interoperability
• ?? Base64 is highly compressible
• ?? IIS 6.0 offers built-in response compression
• ?? Both sides must support it
?? You can write a custom WCF channel to provide compression
Streaming
?? WCF buffers message transmissions by default
• ?? Headers are always buffered
• ?? Body may be buffered or streamed
?? Consider streaming when dealing with large bodies
• ?? Use System.IO.Stream as the input/output type
• ?? It must be the only type in the SOAP body
• ?? Choose a binding that supports streaming
Streaming programming model
[ServiceContract]
Streaming service contract
public interface IPictureServer
{
[OperationContract]
Stream GetPicture(string pictureName);
}
Implementation
internal class PictureServer: IPictureServer {
Stream IPictureServer.GetPicture(string pictureName) {
( gp ){
try {
FileStream reader = new FileStream(pictureName,
FileMode.Open);
return reader;
}
catch (Exception) {
return null;
}
}
}
Streaming configuration
?? Configure a binding that supports streaming
• ?? BasicHttpBinding
• ?? NetTcpBinding
• ?? NetNamedPipeBinding
?? You enabled streaming on the binding via TransferMode
...
Streaming pros/cons
?? Pros
• ?? Avoids buffering the message body
• ?? Writes directly to transport
?? Cons
• ?? Doesn't work with security or RM (both require buffering)
• ?? Recovering from errors is problematic
• ?? Requires .NET on both sides
Good one
Chandrashekar Thota(Editor, MVP)