I am in the process of developing a WCF service that needs to take in an image and 2 parameters. One being an int type, the other a string array. So this would be easy enough if it were only 1 parameter to send up, along with the image:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "UploadImages/{imageID}")]
public void UploadImages(int imageID, Stream image)
{
}
Now, in this scenario the image is in the body of the post. What if the consumer of the service needs to pass up a third piece of data, how does that look and work in WCF?
<system.serviceModel>
<client>
</client>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingStreamed" transferMode="Streamed"></binding>
</webHttpBinding>
</bindings>
<services>
<service name="ImageService">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="MyWebHttpBehavior" name="ImageServiceWebBinding" contract=IImageService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="MyWebHttpBehavior">
<customWebHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="customWebHttp" type="CustomHttpBehaviorExtensionElement, ImageUploader" />
</behaviorExtensions>
</extensions>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
You can pass the additional parameters in the URI as well, like in the example below. Or you can pass them as HTTP headers and fetch them using the
WebOperationContext.Current.IncomingRequest.Headersproperty.Update
Even if the parameter type is an array, you can also pass it in the query string – but you’ll need to provide a
QueryStringConverterwhich can decode that type. The example below shows that.