Working with Streams

While playing around with Web Services, you might have experimented with different types of data that could be returned by a Web Service. If you tried to do some basic functions such as getting a directory listing or finding out some information on a file, you might have run into some problems.

Listings 13.22 and 13.23 give a simple example of how you might attempt to pass back a FileStream object.

Listing 13.22. GetFile WebMethod (C#)
[WebMethod] 
       public FileStream GetFile() 
       {
       FileStream fs = new FileStream("c:\winnt\greenstone.bmp", FileMode.Open); 

       byte[] buf;// = new byte[fs.Length]; 

buf = new Byte[fs.Length]; 
fs.Read(buf,0,(int)fs.Length); 

      return fs; 
      } 

Listing 13.23. GetFile WebMethod (Visual Basic .NET)
      <WebMethod()>public function GetFile() as FileStream 

            FileStream fs = new FileStream("c:\winnt\greenstone.bmp", FileMode.Open); 

            Dim buf() as byte();// = new byte[fs.Length]; 

buf = new Byte(fs.Length); 

      fs.Read(buf,0, fs.Length); 

             return fs; 

             End Function 

The code in Listings 13.22 and 13.23 looks like it should work. But when you try to use it, you end up getting an error. Take a look at the error shown in Figure 13.1 to see what the end result will be.

Figure 13.1. Serialization problems.


This error seems to be very popular with Web Services, but each case needs to be handled in a different way because Web Services need to pass the data in one of two basic forms: text or binary. As for the text format it, needs to be in an XML format of some sort. The data format can be binary or, in this case, an array of bytes.

There is a simple resolution to this problem. Because you are already dealing with an array of bytes, just pass that array back to the client. Take a look at Listings 13.24 and 13.25 to see how this would be done in the code.

Listing 13.24. Returning Binary Data (C#)
[WebMethod] 
             public Byte[] GetFile() 
             {
                   FileStream fs = new FileStream("c:\winnt\greenstone.bmp", FileMode
.Open); 


                   byte[] buf; 

                   buf = new Byte[fs.Length]; 

                   br.Read(buf,0,(int)fs.Length); 
                   fs.Close(); 

                   return buf; 
             } 

Listing 13.25. Returning Binary Data (Visual Basic .NET)
<WebMethod()>public Function GetFile() as Byte() 

             FileStream fs = new FileStream("c:\winnt\greenstone.bmp", FileMode.Open) 


             Dim buf as byte() 

             buf = new Byte(fs.Length) 

             br.Read(buf,0,fs.Length) 

             fs.Close() 

             return buf; 
End Function 

With a simple change to the way the method returns the data, the problem is solved. As you can see, you simply need to change the return type of the method to an array of bytes. Not all your problems will be this simple though.

If you start playing around with the writing of the array of bytes, you might run into the error message shown in Figure 13.2, as we did.

Figure 13.2. Writing to a binary file.


If you take a close look at Listings 13.26 and 13.27, you will notice that we were trying to change the offset of where the file started to write.

Listing 13.26. Offset Error (C#)
private void Button1_Click(object sender, System.EventArgs e) 
       {
       localhost.Service1 svc = new localhost.Service1(); 
       Byte[] ba = svc.GetPicture(); 
       FileStream fs = new FileStream("c:\download\my.bmp",FileMode.Create, 
FileAccess.Write); 

       fs.Write(ba,1,ba.Length);//error on the offset 
       fs.Close(); 

       } 

Listing 13.27. Offset Error (Visual Basic .NET)
private Function Button1_Click(sender as object, e as System.EventArgs ) 

       dim svc as new localhost.Service1() 
       dim ba as byte() 
ba = svc.GetPicture() 
       Dim fs as new FileStream("c:\download\my.bmp",FileMode.Create, FileAccess.Write) 
      fs.Write(ba,1,ba.Length) ‘Error on the offset 
      fs.Close() 

end Function 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset