c# stream wcf上传文件

标签 c# wcf stream

我在 WCF 方面的经验很少,我想通过 WCF 服务(使用流)从客户端机器上传文件到服务器。我阅读了一些主题并自己编写了一个简单的示例,但不幸的是它不起作用

这是一个接口(interface)代码:

[ServiceContract]
public interface IService1
{      
    [OperationContract]
    string UpStream(FileStream inStream);        
}

这是实现:

public string UpStream(FileStream inStream)
    {
        using(StreamReader sr = new StreamReader(inStream))
        {
            var recievedText = sr.ReadToEnd();

            if (recievedText != "")
            {
                return recievedText;
            }
            else
            {
                return "nothing";
            }
        }           
    } 

这是一个客户端代码:

 private void button3_Click(object sender, EventArgs e)
    {
        service2.Service1Client sc = new service2.Service1Client();

        OpenFileDialog opf = new OpenFileDialog();
        opf.ShowDialog();
        if (opf.FileName != "")
        {
            using (FileStream inStream = File.Open(opf.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))           
            {                                                       
                  MessageBox.Show(sc.UpStream(inStream));
            }
        }


    }

我认为问题一定出在配置文件或 Stream 中的某处。当我启动客户端程序并调用 UpStream 方法时,WCF 服务收到一个空流

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services />
    <bindings>
      <basicHttpBinding>
        <binding name="NewBinding0" maxBufferPoolSize="52428800" maxBufferSize="65536000"
          maxReceivedMessageSize="6553600000" transferMode="Streamed"
          useDefaultWebProxy="true" />
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>       
          <serviceMetadata httpGetEnabled="true"/>     
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>  
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

如果有人能帮我解决我的问题,我将不胜感激

最佳答案

流式传输非常有用,但要让您的头脑清醒起来有点棘手

这篇 MSDN 文章提供了很多详细信息 https://msdn.microsoft.com/en-us/library/ms733742%28v=vs.110%29.aspx

但是没有把一些细节说的很清楚

首先你需要传递消息而不是参数

这看起来像

[MessageContract]
public class DataTransfer
{
    [MessageHeader(MustUnderstand = true)]
    public DataContract.HandShake Handshake { get; set; }
    [MessageBodyMember(Order = 1)]
    public Stream Data { get; set; }
    //notice that it is using the default stream not a file stream, this is because the filestream you pass in has to be changed to a network stream to be sent via WCF
}

HandShake 类提供了您需要随流一起包含的参数

public SaveResponse SaveData(DataTransfer request)
{
    using (var stream = new System.IO.MemoryStream())
    {
        request.Data.CopyTo(stream);
        stream.Position = 0;
        //this is because you have less control of a stream over a network than one held locally, so by copying from the network to a local stream you then have more control

接下来是配置:您必须在服务器客户端上配置流式传输

需要这样的东西

<bindings>
  <basicHttpBinding>
    <binding name="ServiceBinding" transferMode="Streamed" messageEncoding="Mtom" maxReceivedMessageSize="67108864" maxBufferSize="65536" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00">
    </binding>
  </basicHttpBinding>
</bindings>

关于c# stream wcf上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37542163/

相关文章:

c# - 在 C# 中开发 winform 应用程序是否有任何标准的屏幕分辨率

c# - 如何将异常序列化为 Json

c# - 具有枚举的 Wcf DataContract 类导致 "' 枚举值 '-1' 对类型无效“错误

wcf - 使用 MVVM 模式将 WPF 网格绑定(bind)到 WCF 服务

c# - C#HttpWebRequest的错误处理

c# - WCF N 层架构

delphi - 如何检查文件是否有备用数据流?

Java 8 流 - 将元素映射到元素对

html - HTML5-音频流

c# - 使用 MemoryMappedViewAccessor 错误编写打包结构?