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
注解
true
设置为 BufferResponse ,将 XML Web 服务方法的响应序列化为内存缓冲区,直到响应完全序列化或缓冲区已满。 缓冲响应后,它会通过网络返回到 XML Web 服务客户端。 false
如果是BufferResponse,对 XML Web 服务方法的响应将发送回客户端,因为它被序列化。 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. 对于较小的数据量,XML Web 服务性能更适合BufferResponsetrue
。
false
何时BufferResponse禁用 XML Web 服务方法的 SOAP 扩展。