简体   繁体   English

C#WCF服务上传文件,实现IHttpHandler,Stream文件

[英]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. 我正在寻找一种将一些文件上传到服务器的方法,并且知道基本的byte []可以解决问题。 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. 因此,我实现了一个基本的上载方法,该方法需要一个byte []并使用流将该byte []附加到文件中,直到发送完所有字节为止。 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. 其次,对于每个较大的文件,每次调用发送大约1024字节的byte []都需要很长时间。 I asked a programmer what a better way would be and he answered with, "use an Ihttphandler and create a stream". 我问一个程序员会有什么更好的方法,他回答说:“使用Ihttphandler并创建一个流”。 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. 现在,我的设置是我有一个wcf服务,该服务正在控制台应用程序中托管。 Then another console app that calls this wcf services upload method. 然后,另一个控制台应用程序调用此wcf服务上载方法。

You can use the Stream class instead of byte[] in your ServiceContract. 您可以在ServiceContract中使用Stream类而不是byte []。

You should set your binding's configuration to transferMode="Streaming". 您应该将绑定的配置设置为transferMode =“ Streaming”。

There are many examples available on MSDN. MSDN上有许多示例。 https://msdn.microsoft.com/en-us/library/ms733742(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/ms733742(v=vs.110).aspx

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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