WebMethodAttribute.BufferResponse Propiedad

Definición

Obtiene o establece si la respuesta para esta solicitud se almacena en el búfer.

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

Valor de propiedad

Boolean

Es true si la respuesta para esta solicitud se almacena en el búfer; en caso contrario, false. De manera predeterminada, es true.

Ejemplos

En el ejemplo de código siguiente se establece la BufferResponse propiedad false en y se controla el streaming de un archivo de texto al cliente. En el ejemplo de código se muestra cómo transmitir un gran fragmento de datos al cliente mediante una clase que implementa la IEnumerable interfaz .

<%@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

Comentarios

trueSi BufferResponse se establece en , serializa la respuesta del método de servicio web XML en un búfer de memoria hasta que la respuesta se serializa completamente o el búfer está lleno. Una vez almacenada en búfer la respuesta, se devuelve al cliente del servicio web XML a través de la red. Cuando BufferResponse es false, la respuesta al método de servicio web XML se devuelve al cliente a medida que se serializa. En general, solo desea establecer en BufferResponse false, si se sabe que un método de servicio web XML devuelve grandes cantidades de datos al cliente. Para cantidades más pequeñas de datos, el rendimiento del servicio web XML es mejor con BufferResponse con .true

Cuando BufferResponse es false, las extensiones SOAP se deshabilitan para el método de servicio web XML.

Se aplica a