Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to enable both session and streaming in netTcpBinding?

Tags:

I writing a WCF Service that need transfer large files, so i using streaming, but from the other hand i need to do username specific initializations.

The problem is that getting the username and perform initialization every time is very expensive. If i could turn on session, i could just save initialized data in local variables in the service instance.

Is there a way to turn on both streaming and session in netTcpBinding?

like image 552
DxCK Avatar asked Jan 15 '10 23:01

DxCK


2 Answers

Large file transfers are indeed a problem in wcf and the streaming option does not solve anything (you even need more memory on the server).

If you don't want to use sockets can solve the problem with implementing your own "protocol" for splitting up the file in blocks and transfer only separate blocks. I used reliableSessions and TransportWithMessageCredential.

The Server (or client) interface looks something like this:

[ServiceContract(CallbackContract = typeof(IClient), SessionMode = SessionMode.Required)]
public interface IServer
{
    [OperationContract]
    FilePart GetFileChunk(string identifier, int number, int blockSize);   
}

As DataContract you can use something like this:

 [DataContract]
public class FilePart
{
    [DataMember] public int Part;
    [DataMember] public byte[] Data;
    [DataMember] public int BlockSize;
}

To find the "right" block size you have to play around a little, i recommend something about 64-512 kb. When they are too small you have a lot request, when they are to large it gets slow and you have more load on the server side.

It's also important that the maxReceivedMessageSize, maxBufferSize, and the timeouts are high enough (in the binding configuration) and the reader quotas. For testing i recommend to use the maximum for all the fields, when it works use values that better fits.

If you work with duplex bindings you can pass objects with by ref. With this way you can pass callback objects, so you are better able to view the progress of the transfer and so on...

[OperationContract IsOneWay=true]
FilePart GetFileChunk(string identifier, int number, int blockSize, ref TransferState callback);   

I think this are all tricks and hints I can give. I hope it helps.

like image 143
CaptainPlanet Avatar answered Oct 11 '22 15:10

CaptainPlanet


I don't think that WCF is appropriate for transferring large files - try using System.Net.Sockets instead, altough WCF is based on them.

like image 40
2 revs Avatar answered Oct 11 '22 15:10

2 revs