WebMethodAttribute.BufferResponse 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定這個要求的回應是否緩衝。
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
屬性值
如果緩衝這個要求的回應,則為 true
,否則為 false
。 預設為 true
。
範例
下列程式碼範例會將 BufferResponse 屬性設定為 false
,並將文字檔的串流處理回用戶端。 此程式碼範例示範如何使用實 IEnumerable 作 介面的類別,將大量資料串流回用戶端。
<%@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
備註
將 設定 BufferResponse 為 true
,會將 XML Web 服務方法的回應序列化為記憶體緩衝區,直到回應完全序列化或緩衝區已滿為止。 緩衝處理回應之後,它會透過網路傳回至 XML Web 服務用戶端。 當 為 false
時 BufferResponse ,XML Web 服務方法的回應會在序列化時傳回用戶端。 一般而言,如果您知道 XML Web 服務方法將大量資料傳回給用戶端,您只想要將 設定 BufferResponse false
為 。 對於較小的資料量,XML Web 服務效能較適合使用 BufferResponse true
。
當 為 false
時 BufferResponse ,XML Web 服務方法會停用 SOAP 延伸模組。