C# 流 WCF 上传文件

  • 本文关键字:文件 WCF c# wcf stream
  • 更新时间 :
  • 英文 :


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

这是一个接口代码:

[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));
            }
        }

    }

我认为问题一定在配置文件或流中的某个地方。当我启动客户端程序并调用上游方法时,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
}

其中,握手类提供了需要随流一起包含的参数

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>

相关内容

  • 没有找到相关文章

最新更新