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.