Skip to content

Download Stream and Files

Gerardo Sista edited this page Apr 13, 2019 · 11 revisions

SignalGo supports streaming file(s) in a very easy way and it's faster than other protocols in sending and receiving files and streams over the network.

Why should we use signalGo streaming system?

SignalGo uses json to serialize and deserialize data. As you know streaming is not supported natively and if you want to send byte[] data to/from server Signalgo should first convert it to json (tipically in form of base64 string), serialize and once data has been received it must be deserialized and converted back fron base64 to byte[]. This is a very slow pattern and lacks rapidly resources, leading easily to a "memory overflow" exception for large files. It's definitively inefficient. SignalGo has its own mechanism to permit to stream large files without these issues. SignalGo creates a new tcp connection inside the established connection and uses it to send or recevive stream data. It doesn't use json for bytes[] streaming! IMPORTANT: you cannot use SignalGo streaming service if your server app is hosted in IIS. Follow these easy step to set up your raw data exchanger with SignalGo!

Step 1 : Write your download method Server Side

How to download stream from client?

In SignalGo's new version all services interfaces and clases will be automatically generated client side directly with Visual Studio extension, so you can use you services very easy.

Note: your download methods must use StreamInfo as return value, you can make generic for StreamInfo to send custom result to client.

Server side write your service methods like the one in this example:

    [ServiceContract("ProfileStorageManagerService", ServiceType.StreamService, InstanceType.SingleInstance)]
    public class ProfileStorageManager
    {
        public StreamInfo<DateTime> DownloadProfileImage(int userId, string fileName)
        {
            //do somthing
            var filePath = "your file path";
            var stream = new StreamInfo<DateTime>();
            if (!File.Exists(filePath))
            {
                //return error when want for example file not found
                stream.Status = System.Net.HttpStatusCode.NotFound;
                return stream;
            }

            var file = new System.IO.FileInfo(filePath);
            stream.Stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            //length is important
            stream.Length = file.Length;
            //your result data
            stream.Data = file.LastWriteTime;
            return stream;
        }
    }

note: SignalGo automatically disposes your stream after sending to client or in csase the client gets disconnected.

Step 2 : register your stream service(s) class(es) Server Side

ServerProvider provider = new ServerProvider();
//another provider plan
provider.RegisterServerService<ProfileStorageManager>();
//start your provider code

Step 3 : call the download method Client Side

  1. In Signalgo's new version all services and classes are automatically generated client side with Visual Studio using extension, so you can use you services very easily.

  2. Use your stream service in client provider (ProfileStorageManager is generated automatically client side with Visual Studio extension code generator):

var clientProvider = new ClientProvider();
//connect to your server
clientProvider.Connect("your server address here");
//clientProvider is your ClientProvider instance for duplex connection
var profileStorageManagerService = new yourServerNamespace.StreamServices.ProfileStorageManager(clientProvider);

//use your service
using(var streamInfo = profileStorageManagerService.DownloadProfileImage(1234, "test file name"))
{
     //use your stream result
     if (streamInfo.Status == System.Net.HttpStatusCode.OK)
     {
          var bytes = new byte[1024];
          var readCount = streamInfo.Stream.Read(bytes, 0, bytes.Length);
          //logics
     }
}
Clone this wiki locally