Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to send multiple files to a WCF service?

We are creating a WCF service that needs from 2-4 image files (small-about 2k-5k each) sent to it as input parameters, as well as about 6 text-field parameters. The data sent back simply consists of a few text values.

I understand a single file can be sent as a stream, but am not sure how to go about sending multiple files.

The clients in this case will be 3rd parties, for whom we would like to make interfacing with our WCF service as simple as possible.

like image 444
alchemical Avatar asked May 13 '09 16:05

alchemical


1 Answers

There are probably lots of ways to accomplish this, but here are my thoughts. Please note that I just included an arbitrary number of strings, you can add/remove as needed.

First, you'll want some sort of "input" object that will be passed to your WCF service that contains your images and string values. Notice the Images property is an array of byte arrays; this is so you can include multiple images.

[DataContract]
public class InputObject
{
    [DataMember]
    public byte[][] Images { get; set; }

    [DataMember]
    public string FirstValue { get; set; }

    [DataMember]
    public string SecondValue { get; set; }
}

Next, you'll want an object that your WCF service will return...

[DataContract]
public class ReturnObject
{
    [DataMember]
    public string FirstValue { get; set; }

    [DataMember]
    public string SecondValue { get; set; }
}

Your ServiceContract will look like this.

[ServiceContract]
public interface IService1
{
    [OperationContract]
    ReturnObject UploadData ( InputObject input );
}

And your Service class like this.

public class Service1 : IService1
{
    public ReturnObject UploadData ( InputObject input )
    {
        // Do your thing with InputObject data

        ReturnObject returnObject = new ReturnObject
                                        {
                                            FirstValue = "MyFirstValue" ,
                                            SecondValue = "MySecondValue"
                                        };

        return returnObject;
    }
}

You'll need to make sure your service's bindings are appropriate for sending image data so increasing some of the default limits is appropriate. Here's an example binding from my service's config.

<wsHttpBinding>
  <binding name="Service1Binding" maxReceivedMessageSize="1000000">
    <readerQuotas maxArrayLength="1000000"
                  maxBytesPerRead="1000000"
                  maxDepth="1000000"
                  maxNameTableCharCount="1000000"
                  maxStringContentLength="1000000"/>
  </binding>
</wsHttpBinding>

Make sure you specify this as your bindingConfiguration for your service.

<endpoint address="" 
          binding="wsHttpBinding" 
          contract="WcfService5.IService1" 
          bindingConfiguration="Service1Binding">

Now all you need to do is reference this from service from your client and call it.

var images = new byte[2][];
images[0] = System.IO.File.ReadAllBytes( @"D:\Development\TestImage.bmp" );
images[1] = System.IO.File.ReadAllBytes( @"D:\Development\TestImage.jpg" );

var input = new InputObject
                {
                    Images = images ,
                    FirstValue = "MyFirstValue" ,
                    SecondValue = "MySecondValue"
                };

var client = new Service1Client();
client.UploadData( input );

Hope this helps you out...

like image 184
Steve Dignan Avatar answered Nov 13 '22 17:11

Steve Dignan