แก้ไข

แชร์ผ่าน


WebMethodAttribute.BufferResponse Property

Definition

Gets or sets whether the response for this request is buffered.

public:
 property bool BufferResponse { bool get(); void set(bool value); };
public bool BufferResponse { get; set; }
member this.BufferResponse : bool with get, set
Public Property BufferResponse As Boolean

Property Value

true if the response for this request is buffered; otherwise, false. The default is true.

Examples

The following code example sets the BufferResponse property to false and handles the streaming of a text file back to the client. The code example demonstrates how to stream a large piece of data back to the client using a class that implements the IEnumerable interface.

<%@WebService class="Streaming" language="C#"%>

using System;
using System.IO;
using System.Collections;
using System.Xml.Serialization;
using System.Web.Services;
using System.Web.Services.Protocols;

public class Streaming {

    [WebMethod(BufferResponse=false)]
    public TextFile GetTextFile(string filename) {
        return new TextFile(filename);
    }

    [WebMethod]
    public void CreateTextFile(TextFile contents) {
        contents.Close();
    }

}

public class TextFile {
    public string filename;
    private TextFileReaderWriter readerWriter;

    public TextFile() {
    }

    public TextFile(string filename) {
        this.filename = filename;
    }

    [XmlArrayItem("line")]
    public TextFileReaderWriter contents {
        get {
            readerWriter = new TextFileReaderWriter(filename);
            return readerWriter;
        }
    }

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

public class TextFileReaderWriter : IEnumerable {

    public string Filename;
    private StreamWriter writer;

    public TextFileReaderWriter() {
    }

    public TextFileReaderWriter(string filename) {
        Filename = filename;
    }

    public TextFileEnumerator GetEnumerator() {
        StreamReader reader = new StreamReader(Filename);
        return new TextFileEnumerator(reader);
    }

    IEnumerator IEnumerable.GetEnumerator() {
        return GetEnumerator();
    }

    public void Add(string line) {
        if (writer == null)
            writer = new StreamWriter(Filename);
        writer.WriteLine(line);
    }

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

}

public class TextFileEnumerator : IEnumerator {
    private string currentLine;
    private StreamReader reader;

    public TextFileEnumerator(StreamReader reader) {
        this.reader = reader;
    }

    public bool MoveNext() {
        currentLine = reader.ReadLine();
        if (currentLine == null) {
            reader.Close();
            return false;
        }
        else
            return true;
    }

    public void Reset() {
        reader.BaseStream.Position = 0;
    }

    public string Current {
        get {
            return currentLine;
        }
    }

    object IEnumerator.Current {
        get {
            return Current;
        }
    }
}
<%@WebService Class="Streaming" language="VB"%>

Imports System
Imports System.IO
Imports System.Collections
Imports System.Xml.Serialization
Imports System.Web.Services
Imports System.Web.Services.Protocols

Public Class Streaming 

    <WebMethod(BufferResponse:=False)> _
    Public Function GetTextFile(filename As String ) As TextFile
      Return New TextFile(filename)        
    End Function

    <WebMethod> _
    Public Sub CreateTextFile(contents As TextFile) 
        contents.Close()
    End Sub

End Class

Public Class TextFile 
    Public filename As String 
    Private readerWriter As TextFileReaderWriter 

    Public Sub New() 
    End Sub

    Public Sub New(filename As String) 
        Me.filename = filename
    End Sub

    <XmlArrayItem("line")> _
    Public ReadOnly Property contents As TextFileReaderWriter
        Get 
            readerWriter = New TextFileReaderWriter(filename)
            Return readerWriter
        End Get
    End Property

    Public Sub Close() 
        If Not (readerWriter Is Nothing) Then
      readerWriter.Close()
        End If
    End Sub
End Class

Public Class TextFileReaderWriter 
   Implements IEnumerable 


    Public Filename As String 
    Private writer As StreamWriter 

    Public Sub New() 
    End Sub

    Public Sub New(myfilename As String ) 
        Filename = myfilename
    End Sub

    Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
        Dim reader As StreamReader = New StreamReader(Filename)
        Return New TextFileEnumerator(reader)
    End Function

    Public Sub Add(line As String) 
        If (writer Is Nothing) Then
            writer = New StreamWriter(Filename)
    End If
        writer.WriteLine(line)
    End Sub

    Public Sub Add(obj as Object)
    
    End Sub

    Public Sub Close() 
        If Not (writer Is Nothing) Then writer.Close()
    End Sub

End Class

Public Class TextFileEnumerator 
  Implements IEnumerator 
    Private currentLine As String
    Private reader As StreamReader

    Public Sub New(reader As StreamReader) 
        Me.reader = reader
    End Sub

    Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
        currentLine = reader.ReadLine()
        If (currentLine Is Nothing) Then
            reader.Close()
            Return False
        Else
            Return True
        End If
    End Function

    Public Sub Reset() Implements IEnumerator.Reset
        reader.BaseStream.Position = 0
    End Sub

    ReadOnly Property Current As object Implements IEnumerator.Current
        Get 
            Return CurrentLine
        End Get
    End Property
End Class

Remarks

Setting BufferResponse to true, serializes the response of the XML Web service method into a memory buffer until either the response is completely serialized or the buffer is full. Once the response is buffered, it is returned to the XML Web service client over the network. When BufferResponse is false, the response to the XML Web service method is sent back to the client as it is serialized. In general, you only want to set BufferResponse to false, if it is known that an XML Web service method returns large amounts of data to the client. For smaller amounts of data, XML Web service performance is better with BufferResponse to true.

When BufferResponse is false, SOAP extensions are disabled for the XML Web service method.

Applies to