简体   繁体   English

c#stream wcf上传文件

[英]c# stream wcf upload file

I have very little experience in WCF and I want to upload file on server from client machine via WCF service (using streaming).我在 WCF 方面的经验很少,我想通过 WCF 服务(使用流式传输)从客户端机器上传服务器上的文件。 I read few topics and wrote a simple example by myself, but unfortunately it's not working我读了几个主题并自己写了一个简单的例子,但不幸的是它不起作用

This is an interface code:这是一个接口代码:

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

This is implementation:这是实现:

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

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

This is a client code:这是客户端代码:

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


    }

I think that problem must be somewhere in config file or in Stream.我认为这个问题一定是在配置文件或 Stream 中的某个地方。 When I start client program and invoke UpStream method, WCF-service is recieving an empty 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>

If anyone can help me with solving my problem, I'll be very grateful如果有人可以帮助我解决我的问题,我将不胜感激

Streaming is very useful but a little tricky to get your head around流媒体非常有用,但有点棘手

this MSDN article provides lots of details https://msdn.microsoft.com/en-us/library/ms733742%28v=vs.110%29.aspx这篇 MSDN 文章提供了很多细节https://msdn.microsoft.com/en-us/library/ms733742%28v=vs.110%29.aspx

but doesn't make some of the details very clear但并没有把一些细节说得很清楚

firstly you need to pass messages rather than parameters首先你需要传递消息而不是参数

this would look something like这看起来像

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

where the HandShake class provides the parameters you need to include along with your stream 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

next is configuration: you have to configure for streaming on both the server and client接下来是配置:您必须在服务器客户端上配置流

which requires something like this这需要这样的东西

<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>

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

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