title: WCF Service application: The operation must have a single parameter whose type is Stream
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/6366489/wcf-rest-webservice-with-stream">WCF Rest Webservice with stream</a>  </p>
  </blockquote>
  
  
  
  <p>I'm developing a WCF .NET Framework 4.0 with C#.</p>
  
  <p>I've created this WCF with this Visual Studio Template:</p>
  
  <p><img src="https://i.stack.imgur.com/04nKb.png" alt="enter image description here"></p>
  
  <p>I need to send an image with two or three parameters. This is all <code>OperationContract</code> that I have (I'm asking for the last one):</p>
  
  <pre><code>[ServiceContract]
  public interface IRestServiceImpl
  {
      [OperationContract]
      [WebInvoke(Method = "GET",
          ResponseFormat = WebMessageFormat.Json,
          BodyStyle = WebMessageBodyStyle.Bare,
          UriTemplate = "orders/")]
      OrderContract[] allOrders();
  
      [OperationContract]
      [WebInvoke(Method = "POST",
          ResponseFormat = WebMessageFormat.Json,
          BodyStyle = WebMessageBodyStyle.Bare,
          UriTemplate = "filteredOrders/")]
      OrderContract[] GetOrders(IdsMessage msg);
  
      [OperationContract]
      [WebInvoke(Method = "POST",
          ResponseFormat = WebMessageFormat.Json,
          BodyStyle = WebMessageBodyStyle.Bare,
          UriTemplate = "completeFilteredOrders/")]
      OrderContract[] LoadCompleteFilteredOrders(IdsMessage msg);
  
      [OperationContract]
      [WebInvoke(Method = "POST",
          ResponseFormat = WebMessageFormat.Json,
          BodyStyle = WebMessageBodyStyle.Bare,
          UriTemplate = "saveEReports/")]
      Boolean SaveEReports(EReportContract[] eReports);
  
      [OperationContract]
      [WebInvoke(Method = "POST",
          ResponseFormat = WebMessageFormat.Json,
          BodyStyle = WebMessageBodyStyle.Bare,
          UriTemplate = "saveEReport/")]
      long SaveEReport(EReportContract eReport);
  
      [OperationContract]
      [WebInvoke(Method = "POST",
          ResponseFormat = WebMessageFormat.Json,
          BodyStyle = WebMessageBodyStyle.Bare,
          UriTemplate = "UploadPhoto/{eReportId}/{imageType}")]
      Boolean UploadPhoto(string eReportId, string imageType, Stream fileContents);
  }
  </code></pre>
  
  <p>And this is <code>Web.config</code>:</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;system.web&gt;
      &lt;compilation debug="true" targetFramework="4.0" /&gt;
    &lt;/system.web&gt;
    &lt;system.serviceModel&gt;
      &lt;services&gt;
        &lt;service name="EReportService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour"&gt;
          &lt;endpoint address="" binding="webHttpBinding" contract="EReportService.IRestServiceImpl" behaviorConfiguration="web"&gt;
          &lt;/endpoint&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;bindings&gt;
        &lt;webHttpBinding&gt;
          &lt;binding maxReceivedMessageSize="2097152" maxBufferSize="2097152" transferMode="Streamed"/&gt;
        &lt;/webHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="ServiceBehaviour"&gt;
            &lt;serviceMetadata httpGetEnabled="true"/&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
        &lt;endpointBehaviors&gt;
          &lt;behavior name="web"&gt;
            &lt;webHttp/&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt;
    &lt;/system.serviceModel&gt;
   &lt;system.webServer&gt;
      &lt;modules runAllManagedModulesForAllRequests="true"/&gt;
    &lt;/system.webServer&gt;
    &lt;connectionStrings&gt;
  
    &lt;/connectionStrings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>When I run service I get the following exception:</p>
  
  <blockquote>
    <p>The operation must have a single parameter whose type is Stream</p>
  </blockquote>
  
  <p>If I do this:</p>
  
  <pre><code>[OperationContract]
  [WebInvoke(Method = "POST",
      ResponseFormat = WebMessageFormat.Json,
      BodyStyle = WebMessageBodyStyle.Bare,
      UriTemplate = "UploadPhoto")]
  Boolean UploadPhoto(Stream fileContents);
  </code></pre>
  
  <p>It works perfectly but I need to send more data with image.</p>
  
  <p>This service is exposed for an <strong>Android</strong> tablet application. The following code show how I'm sending now images to server:</p>
  
  <pre><code>public static Boolean sendImage(String url, String filePath)
  {
      try
      {
          MultiValueMap&lt;String, Object&gt; formData;
  
          Resource resource = new FileSystemResource(filePath);
  
          // populate the data to post
          formData = new LinkedMultiValueMap&lt;String, Object&gt;();
          formData.add(OrderSpringController.FILE, resource);
  
          HttpHeaders requestHeaders = new HttpHeaders();
          requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
  
          // Sending multipart/form-data
          requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
  
          // Populate the MultiValueMap being serialized and headers in an HttpEntity object to use for the request
          HttpEntity&lt;MultiValueMap&lt;String, Object&gt;&gt; requestEntity = 
                  new HttpEntity&lt;MultiValueMap&lt;String, Object&gt;&gt;(formData, requestHeaders);
  
          GsonHttpMessageConverter messageConverter = new GsonHttpMessageConverter();
          List&lt;HttpMessageConverter&lt;?&gt;&gt; messageConverters = new ArrayList&lt;HttpMessageConverter&lt;?&gt;&gt;();
          messageConverters.add(messageConverter);
  
          // Create a new RestTemplate instance
          RestTemplate restTemplate = new RestTemplate(true);
          restTemplate.getMessageConverters().add(messageConverter);
  
          // Make the network request, posting the message, expecting a String in response from the server
          ResponseEntity&lt;Boolean&gt; response = restTemplate.exchange(url, HttpMethod.POST, requestEntity,
                  Boolean.class);
  
          // Return the response body to display to the user
          return response.getBody();
      }
      catch (Exception ex)
      {
          ex.printStackTrace();
      }
      return null;
  }
  </code></pre>
  
  <p>The error occurs when browsing to metadata endpoint (<a href="http://localhost:2351/RestServiceImpl.svc" rel="nofollow noreferrer">http://localhost:2351/RestServiceImpl.svc</a>).</p>
  
  <p><strong>How can I send an image and parameters?</strong></p>