How to use SQL stream for streaming win32 API and WCF streaming support

I use the Sql file stream type to store large files in the backend. I am trying to use WCf to stream a file to clients.

I can get the file descriptor using SQLFileStream (API). Then I try to return this thread. I have a client-side data implementation to retrieve data from a stream.

I can do this for a regular thread stream and a memory stream. Also, if I convert sqlfilestream to memystream, which also works. The only thing that doesn't work is when I try to return sqlfilestream. What am I doing wrong.

I tried both nettcpbinding with streaming support and http binding with MTOM encoding.

This am error message is:


The connection to the connectors has been interrupted. This may be due to an error processing your message or a receive timeout exceeded by the remote host, or a major network problem. The local timneout socket was 00:09:59 ....

Here is my sample code

        RemoteFileInfo info = new RemoteFileInfo();
        info.FileName = "SampleXMLFileService.xml";

        string pathName = DataAccess.GetDataSnapshotPath("DataSnapshot1");

        SqlConnection connection = DataAccess.GetConnection();            

        SqlTransaction sqlTransaction = connection.BeginTransaction("SQLSileStreamingTrans");
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.Transaction = sqlTransaction;
        command.CommandText = "SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()";

        byte[] transcationContext = command.ExecuteScalar() as byte[];

        SqlFileStream stream = new SqlFileStream(pathName, transcationContext, FileAccess.Read);

// byte [] bytes = new byte [stream.Length]; //stream.Read (bytes, 0, (int) stream.Length);

// Stream reeturnStream = stream; // MemoryStream memoryStream = new MemoryStream (bytes);

        info.FileByteStream = stream;

        info.Length = info.FileByteStream.Length;

        connection.Close();

        return info;

    [MessageContract]
    public class RemoteFileInfo : IDisposable
    {
        [MessageHeader(MustUnderstand = true)]
        public string FileName;

        [MessageHeader(MustUnderstand = true)]
        public long Length;

        [MessageBodyMember(Order = 1)]
        public System.IO.Stream FileByteStream;

        public void Dispose()
        {
            if (FileByteStream != null)
            {
                FileByteStream.Close();
                FileByteStream = null;
            }
        }
    }

Any help is appreciated

+3
source share
1 answer

I just decided it for my situation.

My WCF service is installed in InstanceContextMode.PerCall.

, / , . Dispose ( IDisposable, WCF Dispose ).

, .

+2

Source: https://habr.com/ru/post/1746585/


All Articles