V
valentin tihomirov
Noramlly, we have methods like:
void execute(string login, string password, Stream inputStream) {
if (login
assword is wrong)
throw new Exception("login failed");
// forward or process the stream in chunks
}
The WS framework does allow transferring streams (serialized objects, binary
data). However, the Stream class is not a valid web method parameter. WSE3
demonstrates how to (de-)serialize objects to/from XML stream:
// A service to get/put data from/to server
[WebService]
public class WebService1 : WebService {
public WebService1() {}
public XmlSerializableFile GetFile(string fileName) {
return new XmlSerializableFile(fileName);
}
public void PutFile(XmlSerializableFile request) {
Conlose.WriteLine("input stream has been buffered into " +
request.fileName);
}
}
// An object serializable to file
[XmlSchemaProvider("GetMySchema")]
class XmlSerializableFile {
public string fileName;
XmlSerializableFile (string fileName) { this.fileName = fileName; }
public XmlSchema GetSchema() { return null; }
public static XmlQualifiedName GetMySchema(XmlSchemaSet xss) {
return new XmlQualifiedName("base64Binary",
"http://www.w3.org/2001/XMLSchema");
}
/// restore the input stream data into a new file
public void ReadXml(XmlReader r) {
r.ReadStartElement(); // Read the open tag of the encapsulating
element
fileName = TempFileName(); // create a new file
using (FileStream fs = new FileStream(, FileMode.CreateNew)) {
byte[] buf = new byte[1024];
int numRead = 0;
while ((numRead = reader.ReadContentAsBase64(buf, 0, 1024)) > 0)
fs.Write(buf, 0, numRead);
}
r.ReadEndElement(); // Read the close tag of the encapsulating
element
}
/// write a whole file into xml stream
public void WriteXml(XmlWriter w) {
using (FileStream fs = new FileStream(fileName, FileMode.Open,
FileAccess.Read)) {
byte[] buf = new byte[1024];
int numRead = 0;
while ((numRead = fs.Read(buf, 0, 1024)) > 0)
w.WriteBase64(buf, 0, numRead);
}
}
}
You see, the (de)serialization routine processes the whole stream (buffering
it into the memory or into a file). In result, all the featuring advantage
of streaming -- keeping the working set low -- is lost. Who needs such a
streaming? Pheahaps I'm just missing something and you know how to deliver a
stream right into a web method? I think it is normal to process a stream
depending on other method arguments.
void execute(string login, string password, Stream inputStream) {
if (login
throw new Exception("login failed");
// forward or process the stream in chunks
}
The WS framework does allow transferring streams (serialized objects, binary
data). However, the Stream class is not a valid web method parameter. WSE3
demonstrates how to (de-)serialize objects to/from XML stream:
// A service to get/put data from/to server
[WebService]
public class WebService1 : WebService {
public WebService1() {}
public XmlSerializableFile GetFile(string fileName) {
return new XmlSerializableFile(fileName);
}
public void PutFile(XmlSerializableFile request) {
Conlose.WriteLine("input stream has been buffered into " +
request.fileName);
}
}
// An object serializable to file
[XmlSchemaProvider("GetMySchema")]
class XmlSerializableFile {
public string fileName;
XmlSerializableFile (string fileName) { this.fileName = fileName; }
public XmlSchema GetSchema() { return null; }
public static XmlQualifiedName GetMySchema(XmlSchemaSet xss) {
return new XmlQualifiedName("base64Binary",
"http://www.w3.org/2001/XMLSchema");
}
/// restore the input stream data into a new file
public void ReadXml(XmlReader r) {
r.ReadStartElement(); // Read the open tag of the encapsulating
element
fileName = TempFileName(); // create a new file
using (FileStream fs = new FileStream(, FileMode.CreateNew)) {
byte[] buf = new byte[1024];
int numRead = 0;
while ((numRead = reader.ReadContentAsBase64(buf, 0, 1024)) > 0)
fs.Write(buf, 0, numRead);
}
r.ReadEndElement(); // Read the close tag of the encapsulating
element
}
/// write a whole file into xml stream
public void WriteXml(XmlWriter w) {
using (FileStream fs = new FileStream(fileName, FileMode.Open,
FileAccess.Read)) {
byte[] buf = new byte[1024];
int numRead = 0;
while ((numRead = fs.Read(buf, 0, 1024)) > 0)
w.WriteBase64(buf, 0, numRead);
}
}
}
You see, the (de)serialization routine processes the whole stream (buffering
it into the memory or into a file). In result, all the featuring advantage
of streaming -- keeping the working set low -- is lost. Who needs such a
streaming? Pheahaps I'm just missing something and you know how to deliver a
stream right into a web method? I think it is normal to process a stream
depending on other method arguments.