I am developing a WCF web service that needs to be able to upload files among other things.
Currently my method for adding a 'floorplan' item looks like:
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "Floorplan?token={token}&floorplan={floorplan}")]
string XmlInputFloorplan(string token, string floorplan);
I need to alter it so that an image will be uploaded as a part of this call that can be used in a method like:
public static Guid AddFile(byte[] stream, string type);
In this case the byte[]
is the content of the image. The resulting guid is then passed on to the data layer and the addition of the floorplan is finalized.
So I need to figure out two things:
1) How should I alter the XmlInputFloorplan
interface method so that it also allows for an image as a parameter?
2) How do I consume the service after the alteration?
Thanks!
Here is how I solved it:
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "Floorplan")]
XmlDocument XmlInputFloorplan(Stream content);
Expects an input XML like:
<?xml version="1.0" encoding="us-ascii" ?>
<CreateFloorplanRequest>
<Token></Token>
<Floorplan></Floorplan>
<Image></Image>
</CreateFloorplanRequest>
And the Image contains a base 64 encoded string that represents the image file which I convert to byte[] via:
XmlDocument doc = new XmlDocument();
doc.Load(content);
content.Close();
XmlElement body = doc.DocumentElement;
byte[] imageBytes = Convert.FromBase64String(body.ChildNodes[2].InnerText);
In order to allow for this I had to configure the Web.config like so:
<service behaviorConfiguration="someBehavior" name="blah.blahblah">
<endpoint
address="DataEntry"
behaviorConfiguration="web"
binding="webHttpBinding"
bindingConfiguration="basicBinding"
contract="blah.IDataEntry" />
</service>
<bindings>
<webHttpBinding>
<binding name="basicBinding" maxReceivedMessageSize ="50000000"
maxBufferPoolSize="50000000" >
<readerQuotas maxDepth="500000000"
maxArrayLength="500000000" maxBytesPerRead="500000000"
maxNameTableCharCount="500000000" maxStringContentLength="500000000"/>
<security mode="None"/>
</binding>
</webHttpBinding>
</bindings>