1

My company is using a WCF service (ContractsService) for the first time and all attempts to an existing (known-working) pdf creation service (PrintContractService) are failing. PrintContractService returns a byte[] which we must return as a Base64 string.

I've written a client which consumes PrintContractService directly and generates the pdf as expected. It's only when I call ContractsService, get the resulting Base64 string, then process the returned string back into a byte[] that it fails. That's why I think it must have something to do with the WCF endpoint. I've changed the endpoint several times with differing response formats and method types, and I've also tried return types of byte[] and Stream - so far with no good results.

The failure message is "Adobe could not open because it is either not a supported file type or because the file has been damaged ...".

code in client which consumes PrintContractSerivceDirectly (working fine):

    PrintContractService.PrintContractService pcs = new PrintContractService.PrintContractService();
    var d = pcs.PrintContract(Convert.ToInt32(txtContractId.Text), 0, null);
    Context.Response.Expires = 0;
    Context.Response.Expires = 0;
    Context.Response.Buffer = true;
    Context.Response.ClearContent();
    Context.Response.AddHeader("content-dispostion", "inline; filename=" + "contract.pdf");
    Context.Response.ContentType = "application/pdf";
    string s = Convert.ToBase64String(d.Contract);              // <- this works
    Context.Response.BinaryWrite(Convert.FromBase64String(s));  //
    if (d.IsValid)                                          
        Context.Response.BinaryWrite(d.Contract);               // <- this also works
    else
        Context.Response.Write(d.ErrorMessage);

endpoint in ContractsService:

    [OperationContract(Name="registerWithPdf")]
    [WebInvoke(Method="POST"
        , BodyStyle = WebMessageBodyStyle.Bare
        , UriTemplate="registerWithPdf")]
    string RegisterWithPdf(Stream xmlIn);

code in ContractsService.RegisterWithPdf():

        PrintContractService.PrintContractService pcs = new PrintContractService.PrintContractService();
        var d = pcs.PrintContract(masterServiceOps.ServiceParameters_Register.iNewContractId, 0, null);
        string sout = Convert.ToBase64String(d.Contract);   // .Contract is byte[]
        return sout;

code in client which consumes ContractsSerivce (fails every time):

WebResponse resp = req.GetResponse();
using (Stream resStream = resp.GetResponseStream()) {
    StreamReader rdr = new StreamReader(resStream, Encoding.ASCII);
    string s = rdr.ReadToEnd();
    Context.Response.Expires = 0;
    Context.Response.Expires = 0;
    Context.Response.Expires = 0;
    Context.Response.Buffer = true;
    Context.Response.ClearContent();
    Context.Response.AddHeader("content-dispostion", "inline; filename=" + "contract.pdf");
    Context.Response.ContentType = "application/pdf";

    Context.Response.BinaryWrite(Convert.FromBase64String(s));
    Context.Response.End();

}

Thanks in advance for any help. Please note that this must be done as described with a Base64 string. If you have suggestions for other approaches feel free to make them but please only do so if you can also answer this specific question. Thank you.

  • This could be related: http://stackoverflow.com/questions/7634113/is-it-possible-to-get-data-from-web-response-in-a-right-encoding – yms Mar 10 '15 at 15:57
  • Update: The service was throwing '\' characters in the returned string. With those removed (on the client side) I get the PDF as expected. Working now on finding out how to lose them. – Scott Rushing Mar 10 '15 at 18:58
  • I still suspect your problem is related to incompatible encodings, can you try using something like ((HttpWebResponse)webRequest.GetResponse()).CharacterSet instead of Encoding.ASCII ? – yms Mar 10 '15 at 20:22

0 Answers0