开发者

WCF Function won't return stream

开发者 https://www.devze.com 2023-03-05 01:12 出处:网络
So I have a function which returns a pdf stream. The calling procedure gets the stream and writes it to disk (thanks ServiceGuy!). Normal VB code is as follows:

So I have a function which returns a pdf stream. The calling procedure gets the stream and writes it to disk (thanks ServiceGuy!). Normal VB code is as follows:

Public Function GetPDF() As System.IO.FileStream

    GetPDF = File.OpenRead("C:\Documents and Settings\jspel903\Desktop\3211LD.开发者_如何学Cpdf")

End Function

Now, for whatever reason when I put that code in my WCF (see below) and build/install, it won't return anything. The calling procedure gets nothing after My WCF is hosted in a Windows service (I can't get it to debug or step into). So I'm wondering if, perhaps, WCFs won't return a FileStream?

Here's the IService:

<ServiceContract()> _
Public Interface IService1

<OperationContract()> _
Function GetPDF() As System.IO.FileStream
'Should return a .pdf file as a stream

End Interface

Here's the svc:

Imports System.IO

Public Class Service1
Implements IService1


Public Function GetPDF() As System.IO.FileStream Implements IService1.GetPDF

    GetPDF = File.OpenRead("C:\Documents and Settings\jspel903\Desktop\3211LD.pdf")

End Function
End Class

Seems to me like it should work. Any ideas?


You probably want to convert your FileStream to a byte array and return that. WCF is generally limited to returning serializable types.

   // fs is your FileStream

   byte[] Data = new byte[fs.Length];

   fs.Read(Data,0,fs.Length);


It's not going to work... Think about what it would mean if it did. Your WCF client would have access to a file stream on your server device. It'd have to be able to perform all of the operations that you could do locally, over a remote connection.

WCF is a transport mechanism for data, it doesn't actually send object references to the server's objects. The client gets a copy of the object, that has been serialized by the server, then deserialized at the client side. If you could send a stream object, the file handle / memory reference etc wouldn't mean anything to the receiving client.

You need to read the data from the stream on the server and then convert it into an appropriate data object to transmit back to the client.

EDIT:

Apparently you can use streaming with WCF, but you're not going to be able to return a System.IO.FileStream, you can return a Stream.

Take a look at: Large Data And Streaming for a description and some of the restriction's you'll need to be aware of if you take that approach.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号