avidnov
avidnov

Reputation: 35

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). 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. When I start client program and invoke UpStream method, WCF-service is recieving an empty stream

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

Upvotes: 3

Views: 7709

Answers (1)

MikeT
MikeT

Reputation: 5500

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

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

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>

Upvotes: 2

Related Questions