Streams

V

valentin tihomirov

Noramlly, we have methods like:

void execute(string login, string password, Stream inputStream) {

if (login:password 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.
 
J

John Saunders [MVP]

valentin tihomirov said:
Noramlly, we have methods like:

void execute(string login, string password, Stream inputStream) {

if (login:password 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.

It may very well be normal to process streams in this manner. However, it is
not normal for the standard ASMX web service platform, as the basic
WSDL/XSD/WS-I BP-1 standards do not include streams of any kind.

Perhaps WCF can offer something like this.
 
V

valentin tihomirov

It may very well be normal to process streams in this manner. However, it
is
not normal for the standard ASMX web service platform, as the basic
WSDL/XSD/WS-I BP-1 standards do not include streams of any kind.

It is just silly to stream between two large buffers. The feature of streams
is to replace the buffering with processing in parts.
 
J

John Saunders [MVP]

valentin tihomirov said:
It is just silly to stream between two large buffers. The feature of
streams is to replace the buffering with processing in parts.

You are correct, of course.

This just proves that the basic Web Services standards are not appropriate
for streaming applications. Perhaps one of the WS-* standards covers that.
 
V

valentin tihomirov

This just proves that the basic Web Services standards are not appropriate
for streaming applications. Perhaps one of the WS-* standards covers that.

Have you seen this:
Yasser Shohoud - "Web services and large content in .NET 2.0"
http://blogs.msdn.com/yassers/archive/2004/11/10/255212.aspx

WSE3.0 -- "How to: Stream Large Amounts of Data from a Web Service"
http://msdn2.microsoft.com/en-us/library/aa528818.aspx

???

Agreed on incompatibility between streams and web services, the microsoft
developments are deceptive.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top