Fred Fredson

Hi,

I'm trying to pass a IO.Stream as a parameter to a Web Service but I am coming under a little difficulty. Both the calling application and the Web Service application recognize the IO.Stream object but when I try and call the WebMethod it tells me that it is expecting a WebService_Stream data type instead of an IO.Stream datatype.

I've done some research and seen suggestions to "Serialize" data objects- but the examples I have seen have all been for user defined classes on the Web Service side.

Is there an easier or better way to do this

I'm using 2.0 framework. I've noticed some references in 3.0 to a new way of doing services using the Windows Communication Foundation, would this solve my problem Is it easier to pass objects with this

Many thanks in advance.




Re: .NET Base Class Library Passing a Stream to a Web Service

ScubaSteve20001

As far as I know you cannot pass a stream as a parameter to a web service b/c it is not serializable. You will need to find another way to pass your data along.




Re: .NET Base Class Library Passing a Stream to a Web Service

timvw

There area couple of options:
- Return a byte[]
- Implement an IHttpHandler instead and use the context.Response.OutputStream to push it outside (and read from the HttpWebRequest ResponseStream)





Re: .NET Base Class Library Passing a Stream to a Web Service

Fred Fredson

Thank you for the responses!

Do you have any examples of using an IHttpHandler in this manner I'm not sure I completely follow what I need to do.






Re: .NET Base Class Library Passing a Stream to a Web Service

timvw

Do take in consideration that you're not using a Webservice anymore (simply add a new Item -> Generic Item to your webproject)...

Code Snippet

public class MyStreamHandler : IHttpHandler

{
public void ProcessRequest(HttpContext context)
{
Stream yourStream = null; // the stream you want to send


context.Response.ContentType = "application/octet-stream";
StreamHelper.Copy(yourStream, context.Response.OutputStream, 1024);
}

public bool IsReusable
{
get { return true; }
}
}


And now implement a client to consume:

Code Snippet
static void Main(string[] args)
{
Stream yourStream = null; // the stream you have to receive...

WebRequest req = WebRequest.Create("http://example.com/StreamHandler.ashx");
WebResponse rsp = req.GetResponse();
using (Stream input = rsp.GetResponseStream())
using (Stream output = yourStream))
{
StreamHelper.Copy(input, output, 1024 * 1000);
}
}

Console.Write("{0}Press any key to continue¡­", Environment.NewLine);
Console.ReadKey();
}


And if you were wondering, the StreamHelper

class StreamHelper
{
public static void Copy(Stream input, Stream output, int bufferSize)
{
byte[] buffer = new byte[bufferSize];
int bytesRead;

while ((bytesRead = input.Read(buffer, 0, bufferSize)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}

}





Re: .NET Base Class Library Passing a Stream to a Web Service

Fred Fredson

Ahhh... great... I see what you are doing! I'll give that a try!

Thanks!