Subscribe to Subscribers

Online Members

Shine S
More...

Resources » .NET programming » WCF/Webservices

Binary data in WCF


Posted Date:     Category: WCF/Webservices    
Author: Member Level: Silver    Points: 4



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
...


transferMode="StreamedResponse"/>



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





Did you like this resource? Share it with your friends and show your love!


Responses to "Binary data in WCF"
Author: ChandraShekar Thota    26 Jan 2009Member Level: Gold   Points : 2
Good one

Chandrashekar Thota(Editor, MVP)



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Hosting WCF and ASP.Net Compatability Mode
    Previous Resource: WCF
    Return to Resources
    Post New Resource
    Category: WCF/Webservices


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Binary data in WCF  .  

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2013 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.