4

In WCF, contracts can be switched to streaming mode, to transfer large messages.

After reading and testing it seems to me, that streaming mode can not be used with duplex channels (channels with OneWay-calls and a callback interface).

Is this so? Do duplex and streaming can not be used with each other? Or is there a way?

(I'm trying to upload a large file to the service and use callback to report progress on this)

Sam
  • 28,421
  • 49
  • 167
  • 247

2 Answers2

5

to load the file from the client to the server, you can use the following code: service

 [ServiceContract(SessionMode=SessionMode.Required, CallbackContract=typeof(IFreeBoxServiceCallBack))]
    public interface IFreeBoxService
    {
        [OperationContract(IsOneWay = true)]
        void sendFile(byte[] bytes, int offset, int count);

        [OperationContract(IsOneWay = true)]
        void sendFileName(string fileName);
    }

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode=ConcurrencyMode.Multiple)]
    public class FreeBoxService:IFreeBoxService
    {
        string path = "D:\\repository\\";
        string fileName = "";
        IFreeBoxServiceCallBack callBack = null;

        public FreeBoxService()
        {
            callBack = OperationContext.Current.GetCallbackChannel<IFreeBoxServiceCallBack>();
        }

        public void sendFileName(string fileName)
        {
            this.fileName = fileName;
        }

        public void sendFile(byte[] bytes, int offset, int count)
        {
            using (FileStream fileStream = new FileStream(path + fileName, FileMode.Append, FileAccess.Write))
            {
                fileStream.Write(bytes, offset, count);
                fileStream.Close();
                fileStream.Dispose();
            }
        }}

client:

private void sendFileToServer()
        {
            FileStream fs = new FileStream(fullName,FileMode.Open,FileAccess.Read);
            int bytesRead = 0;
            bytes = new byte[15000];

            proxy.sendFileName("someFile.xml");

            while ((bytesRead = fs.Read(bytes, 0, 15000))>0)
            {
                proxy.sendFile(bytes, 0, bytesRead);
            }
            fs.Close();
            fs.Dispose();
        }

.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="FreeBoxServiceLib.FreeBoxService" behaviorConfiguration="MyServiceBehevior">
        <endpoint address="" contract="FreeBoxServiceLib.IFreeBoxService"
                  binding="netTcpBinding">
        </endpoint>
        <endpoint address="MEX" binding="mexTcpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8080/FreeBoxService/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehevior">
          <serviceMetadata />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

is always better to pass an array of bytes. I think, not need describe the callback functions?

2

Out of curiosity I was about to start some testing on your question but then Google revealed to me the two samples which might answer your question better.

This CodeProject example shows streaming file transfers with a progress bar without using Duplex channels.

This sample shows more of the same but with some different disposing of the stream.

Also, a really good resource for all things WCF related is iDesgin.net. The main guy there is Juval Lowy who wrote some of the best books regarding WCF. They have dozens of excellent WCF examples you can download (although they annoyingly ask your for your email address for each one). More importantly, they also wrote a ServiceProcessEx class which greatly extends what ServiceProcess can do, especially in regards to Duplex channels. (I'm not sure if it deals much with streaming though... its not something I've done yet).

Hope some of this is helpful to you.

Sailing Judo
  • 11,083
  • 20
  • 66
  • 97