简体   繁体   中英

C# WCF Service to upload files, implementing IHttpHandler, Stream files

I was looking around at a way of uploading some files to a server and understood that a basic byte[] would do the trick. So I implemented a basic upload method that takes a byte[] and using a stream appends that byte[] to a file until all of the bytes have been sent. However, this has two problems. Firsty, it is extremely slow, the handshaking process takes too much time. Secondly, sending byte[]'s over that are around 1024 bytes each call takes a long time for bigger files. I asked a programmer what a better way would be and he answered with, "use an Ihttphandler and create a stream". That didn't make much sense to me.

Can anyone show me could or point me in the right direction for creating a connection to a server that doesn't require handshaking for each client call that can upload large or small files to a server? I understand this question has no code with it, that is because I don't know where to start to create a streamed form of upload. An acceptable answer would even be to point me in some directions of creating my own code. I have read this and because of no code examples was very confused.

My setup right now is that I have a wcf service that is being hosted inside a console application. Then another console app that calls this wcf services upload method.

You can use the Stream class instead of byte[] in your ServiceContract.

You should set your binding's configuration to transferMode="Streaming".

There are many examples available on MSDN. https://msdn.microsoft.com/en-us/library/ms733742(v=vs.110).aspx

<system.serviceModel>
 …
<bindings>
  <basicHttpBinding>
    <binding name="ExampleBinding" transferMode="Streaming"/>
  </basicHttpBinding>
</bindings>
 …

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM