1

I have a WCF service that will receive an id and return a pdf file based off of the id. I am assuming that the best way to do this would be to convert the pdf file to some sort of string and then return it. However, I am unsure of which stream to use (Stream, Filestream, or MemoryStream).

I know i could read the pdf file to a byte array and then do something with it using MemoryStream, but I feel like there is a more efficient way.

I tried doing what was suggested in this post here: How to convert a pdf to a memory stream

Hard coding a file path in to return like so:

return File(@"C:\MyFile.pdf", "application/pdf");

Just to see what happens, but get an error on File saying: `'System.IO.File' is a 'type' but is used like a 'variable'.

Would anyone be able to provide a little insight?

EDIT

This is currently what I am working towards:

[OperationContract]
[WebInvoke(Method = "GET",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "/GetInvoiceFile")]
Stream GetInvoiceFile(string id);

public Stream GetInvoiceFile(string BillingPeriodId)
{
    byte[] bytes = File.ReadAllBytes(@"C:\pdf-test.pdf");
    return new MemoryStream(bytes);
}

I just dont exactly know how I would edit my OperationContract to return the pdf. Any ideas?

Community
  • 1
  • 1
scapegoat17
  • 5,509
  • 14
  • 55
  • 90

1 Answers1

2

The answer depends on what you have. You seem to have the path to a file as a string. Probably, your WCF method returns a byte[]. You somehow need to convert that path to a byte[] efficiently.

You can do this using File.ReadAllBytes(string path).

WCF also supports streaming writes but that is a little more complicated and the performance wins are dubious for small files.

The fact that you tried to specify a MIME type ("application/pdf") tells me that you do not really understand the difference between a WCF service and an HTTP service. Researching this is homework for you.

usr
  • 168,620
  • 35
  • 240
  • 369
  • Thank you for the response. I do already have the file set to a `byte[]`. I think i would like to write it to a stream for now at least. What would be the best way to achieve this? – scapegoat17 Mar 25 '15 at 20:35
  • What does your WCF method return? A byte[]? Your fixation on streams seems unfounded. – usr Mar 25 '15 at 20:38
  • To answer your question literally: The most performant and easiest way to convert a byte[] to a Stream is new MemoryStream(myByteArray). I doubt this is what you need, though. – usr Mar 25 '15 at 20:39
  • Currently I have my method set up to return a Stream. – scapegoat17 Mar 25 '15 at 20:42
  • OK. Make it return a byte[]. If you don't want that for some reason use the stream solution that I provided. This will *not* give you streaming semantics since you're loading the entire file into memory first. Does this answer your question fully? – usr Mar 25 '15 at 20:49
  • To give you a better idea with what i am working with please see my edit above. I understand the `OperationContract` is not correct. I am unsure how i would adjust it though. I have not worked with WCF that much prior to this so it is all a learning experience. Any suggestions? – scapegoat17 Mar 25 '15 at 21:05
  • I'm not familiar with WebInvoke. I can't answer that part of the question and I would not have posted an answer if I had know that this is the real problem for you. I have answered the stream part though: new MemoryStream(myByteArray) – usr Mar 25 '15 at 21:08
  • 1
    As far as answering my question goes, you have helped a great deal. You just seemed knowledgeable so i figured i would ask. Thank you for the help. – scapegoat17 Mar 25 '15 at 21:16