Can I use WCF services for uploading and downloading?
I'm working on a media server project for school, and I've chosen the WCF approach for this. However I'm starting to wonder whether or not thi开发者_如何学编程s is a good idea.
Is getting a WCF service to upload and download files a straightforward thing to do? By straightforward, I mean something that doesn't involve a lot of hacks or third party libraries. The WCF service will also be expected to stream both audio and video to its consumers/clients.
I should also mention that the WCF service in question is being hosted as a Windows Managed Service outside of the IIS.
This is sort of what I have in mind right now:
[OperationContract]
bool AddFile(byte[] filedata);
[OperationContract]
bool DeleteFile(int fileid);
[OperationContract]
byte[] GetFile(int fileid);
Define the methods to take and return the Stream
data type and make sure to configure your binding to use TransferMode.Streamed
. Also make sure to be concious of the encoding you use on your binding. If you use Text
all binary data will be base64 encoded which will bloat the size of all your requests/responses. If you use MTOM then large binary data like streams will be transferred outside the SOAP body as a separate entity of the HTTP body as pure, unencoded binary data. Binary encoding will obviously provide the most optimal transmission over the wire, however it is not a standard format, so interop with other, non-WCF stacks will not be possible. If you'r pure WCF, you can't beat the performance of binary message encoding though, so I highly recommend it or fallback to MTOM if you require interop.
Also, I've somewhat assumed thus far you are trying to do this within the realm of SOAP. If you're using REST then the XML vs. MTOM vs. Binary encoding would be alleviated because you could just send/receive a pure binary HTTP body with the appropriate content-type. All depends on what you're trying to do
精彩评论