wcf 流式处理大型文档限制为 1536 字节

wcf Streaming large documents limited to 1536 Bytes

提问人:Yassir Yaghzar 提问时间:9/27/2023 更新时间:9/27/2023 访问量:13

问:

我有一个旧版 wcf 服务,我正在开发该服务以启用流式处理大型文档。在客户端中,无论我设置什么缓冲区来读取流,我都被限制为只有 1536 字节。它一次只能读取 1536 字节。所以获取文档需要很长时间。

我确保我的传输模式在客户端和服务器中都设置为流式处理,并将 maxVytesPerRead 设置为 max。我还在 MTOM 和 TEXT 之间切换

服务

Public Shared Function GetDocumentByGuid(ByVal DocID As String, ByVal Criteria As ProjectSearchTemplate) As DocumentDisplay
            Dim documentDisplayItem As DocumentDisplay
            Dim enumerator As IEnumerator = Nothing
            Dim contentSize As Nullable(Of Double)
            Dim fileNetConnect As FileNetConnect = New FileNetConnect(Criteria.UserName, Criteria.Password, Criteria.Url)
            If (fileNetConnect Is Nothing AndAlso Not String.IsNullOrEmpty(Criteria.UserName) AndAlso Not String.IsNullOrEmpty(Criteria.Password) AndAlso Not String.IsNullOrEmpty(Criteria.Url)) Then
                fileNetConnect = New FileNetConnect(Criteria.UserName, Criteria.Password, Criteria.Url)
            End If
            If (Not String.IsNullOrEmpty(Criteria.SelectedSrcObjectStore) AndAlso fileNetConnect IsNot Nothing) Then
                Dim objectStores As IObjectStoreSet = fileNetConnect.GetObjectStores()
                Try
                    enumerator = objectStores.GetEnumerator()
                    While enumerator.MoveNext()
                        Dim current As IObjectStore = DirectCast(enumerator.Current, IObjectStore)
                        If (Operators.CompareString(current.SymbolicName, Criteria.SelectedSrcObjectStore, False) <> 0) Then
                            Continue While
                        End If
                        Dim document As IDocument = fileNetConnect.FetchDocument(current, DocID)
                        If (document Is Nothing) Then
                            Continue While
                        End If
                        Dim binaryReader As System.IO.BinaryReader = New System.IO.BinaryReader(document.AccessContentStream(0))
                        Dim docStream As New MemoryStream()
                        document.AccessContentStream(0).CopyTo(docStream)
                        docStream.Seek(0, SeekOrigin.Begin)
                        Dim documentDisplayItem1 As DocumentDisplay = New DocumentDisplay() With
                        {
                            .Id = DocID,
                            .MimeType = document.MimeType,
                            .Content = docStream
                        }

                        documentDisplayItem = documentDisplayItem1
                        Return documentDisplayItem
                    End While
                Finally
                    If (TypeOf enumerator Is IDisposable) Then
                        TryCast(enumerator, IDisposable).Dispose()
                    End If
                End Try
            End If
            documentDisplayItem = Nothing
            Return documentDisplayItem
        End Function
 <MessageContract>
    Public Class DocumentDisplay

        <MessageHeader>
        Public Property Id As String

        <MessageHeader>
        Public Property MimeType As String

        <MessageBodyMember>
        Public Property Content As Stream

        Public Sub Dispose()
            If Content IsNot Nothing Then
                Content.Close()
                Content = Nothing
            End If
        End Sub


    End Class

    <MessageContract>
    Public Class DownloadRequest
        <MessageBodyMember>
        Public Id As String
    End Class
<bindings>
            <basicHttpBinding>

                <binding name="TransferService"
                   maxReceivedMessageSize="2147483647"
                   maxBufferSize="2147483647" transferMode="Streamed"  >

                    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                         maxArrayLength="2147483647" maxBytesPerRead="2147483647"
                         maxNameTableCharCount="2147483647"/>
                </binding>

客户

 Dim request As New DownloadRequest With {
                .Id = DocID
            }

            Dim document = proxy.ICNJavaScriptProxy_IICNJavaScriptProxy_GetDocumentByGuid(request)

            Dim bytesRead As Integer = 0
            Dim chunkSize As Integer = 100000000 ' Adjust the chunk size as needed 100MB
            Dim buffer(chunkSize - 1) As Byte
            bytesRead = document.Content.Read(buffer, 0, buffer.Length)
            DEEPDirAppend.Main(logFilepath, String.Concat("bytesRead", bytesRead, DateTime.Now.ToString()), logName)
            MyBase.Response.Clear()
            MyBase.Response.BufferOutput = False
            MyBase.Response.ClearHeaders()
            MyBase.Response.ContentType = document.MimeType
            Dim str As String = document.Id.Replace("}", "").Replace("{", "").ToString()
            MyBase.Response.AddHeader("Content-Disposition", String.Concat("inline;filename=", str))
            While bytesRead > 0

                If Response.IsClientConnected Then
                    Response.OutputStream.Write(buffer, 0, bytesRead)
                    Response.Flush()
                    buffer = New Byte(chunkSize - 1) {}
                    bytesRead = document.Content.Read(buffer, 0, buffer.Length)
                Else
                    bytesRead = -1
                End If
            End While


            Response.Flush()
            Response.Close()
            Response.End()

<binding name="BasicHttpBinding_ITransferService" closeTimeout="04:01:00"
     openTimeout="04:01:00" receiveTimeout="04:10:00" sendTimeout="04:01:00"
     allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
     maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
     textEncoding="utf-8" transferMode="Streamed" useDefaultWebProxy="true">
     <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
      maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
     <security mode="None">
      <transport clientCredentialType="None" proxyCredentialType="None"
       realm="" />
      <message clientCredentialType="UserName" algorithmSuite="Default" />
     </security>
    </binding>
.NET WCF 式处理内存流

评论

0赞 Jiayao 9/27/2023
怎么样 ?增加它可以提高发送大型消息时的吞吐量。您可以注意文件传输过程中的内存使用情况。ConnectionBufferSize

答: 暂无答案