views:

77

answers:

2

I have an irritating problem, I'm creating an asp.net web service; this service should expose the functionality of an existing library, the service allows users to Upload files to our server by invoking the UploadFile method on the service. Here's the signature of the UploadFile method

public bool UploadFile(string bucketName, string desiredFileName, System.IO.Stream fileData)


Now, when I try to invoke this method and pass it a System.IO.Stream object referring to the file to be uploaded, I get a compile time error, indicating the the passed in type (System.IO.Stream) is not the expected type (Mynamespace.ServiceReference.Stream). I tried to explicitly cast my stream to the type (Mynamespace.ServiceReference.Stream) but the compiler wouldn't let me do it either, though they are of the same type! weired!

PS: I'm new to web services.

+1  A: 

The problem is in order to generate the WSDL the Stream will attempt to be serialized which it can't do.

Your original idea of a byte[] is the correct way to do it.

You do not need to bother with Base64 conversion because the ASP.NET runtime will do this for you already.

Lloyd
So no way to pass a System.IO.Stream as the service requires!?? and why was that conflict anyway?
Galilyou
The conflict was because the WSDL generator/importer will attempt to create a serializable type from System.IO.Stream.To see this in action create a dummy class, Bob, and add method in your web service which users or returns it.You should where possible stick to primitive types and structs.
Lloyd
A: 

Here's the Solution.

Instead of having byte[] in argument for file data, use base64 string represntation of the byte[].

from client that consumes your webservice, let it convert the byte[] to base64 string and then pass this to your webservice call. At the server end you should receive the base64 string of the byte array and convert it back into byte[].

Here are the references for conversion methods within the .Net Framework.

From byte[] to Base64 - For use in webservice clients

link text: http://msdn.microsoft.com/en-us/library/dhx0d524.aspx

From Base64 to byte[] - For use in webservice server

link text: http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx

this. __curious_geek
So no way to pass a System.IO.Stream as the service requires!?? and why was that conflict anyway?
Galilyou
The file stream is always only accessible on single machine. We can not have streamReader or streamWriter remotely for the File I/O, Before that there must be a NetworkStream to exchange the data and then have a local file stream to write that data.
this. __curious_geek