.Net ramblings
# Wednesday, 23 June 2004
WSE 2.0 send DIME attachments from client TO a web service

I wanted to improve the performance of my winforms app sending binary data to a web service, so i chose to upgrade to WSE 2.0 and use the DIME specification. 

I checked out the examples in the docs, but only found one with a web service sending an attachment to a client.  i wanted it the other way around.  i tried to modify it to suit my own needs, but couldn't get it to work. (in the end i had the response and request contexts mixed up). I searched high and low online and finally found that there is a sample as part of the WSE full installation which contains just what i wanted.  I could find no mention of this sample in the docs, so I am posting it here.

The code below shows the important methods, on the client and on the server.  It assumes a web project and winforms project both set up for WSE2.0. 

// this is part of a winforms application with an openfiledialog
private void btnUpload_Click(object sender, System.EventArgs e)
  if(this.openFileDialog1.ShowDialog() == DialogResult.OK)
    DimeAttachment dimeAttach = new DimeAttachment("image/gif", TypeFormat.MediaType,this.openFileDialog1.FileName);
    ws = new TestService.Test();
    MessageBox.Show("Upload successful");
    MessageBox.Show("No file selected");

// web service method...
public void ReceiveImage(string filename)
  // Reject any requests which are not valid SOAP requests
  if (RequestSoapContext.Current == null)
    throw new ApplicationException("Only SOAP requests are permitted.");
  if (RequestSoapContext.Current.Attachments.Count == 0)
    throw new ApplicationException("No attachments were sent with the message.");

    FileStream f = System.IO.File.OpenWrite(Server.MapPath("Upload/" + filename));
    Stream s = RequestSoapContext.Current.Attachments[0].Stream;
    byte[] bytes = new byte[s.Length];
    s.Read(bytes, 0, bytes.Length);
    f.Write(bytes, 0, bytes.Length);
  catch(Exception ex)
    throw ex; // or log it...

Wednesday, 23 June 2004 13:08:19 (GMT Daylight Time, UTC+01:00)  #    Comments [1]  .Net General