Returning a pdf-file from a webservice

I am returning a pdf-file from a webservice. I am thinking of just returning it as a byte array, but I am wondering whether there is a better way to return it. Speed is an issue and the pdf-file is quite small, so I have to avoid a solution which requires returning much more data.




Answer this question

Returning a pdf-file from a webservice

  • Salvador Dali

    Treat PDF as binary, i.e., byte array, is the fastest way to get it from web service.
    If you want faster than web service, you can write a Http handler, ashx, and an ashx client to invoke the ashx, to get it as binary, not base64 string as in SOAP. 

    Web method uses base64 string for binary data. Base64 encoding turns 3 bytes into 4 characters, 4 bytes, so the binary data in SOAP is 1/3 larger. Communication over Http, data size should be kept as small as possible. The encoding/decoding process also takes time.

    For all .NET applications, Http handler is the fastest way of moving data in and out of IIS, ashx can be called from your own code, not just used in browser. The server and client side SOAP pipelines implemented for web services are much more complex than ashx processing, web services are clearly slower than ashx.

    I choose ashx over asmx for my .NET Http communication system, I call it "Remote Object Invocation Framework", it's secure, efficient, and easy to use. I used it for my "WebSql Data Provider" for remote SQL database access, both are working great. If returning PDF in my way, it's just binary, no base64 involved.       

  • Returning a pdf-file from a webservice