Possible Duplicate:
WCF Rest Webservice with stream
I'm developing a WCF .NET Framework 4.0 with C#.
I've created this WCF with this Visual Studio Template:
I need to send an image with two or three parameters. This is all OperationContract
that I have (I'm asking for the last one):
[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);
}
And this is Web.config
:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="EReportService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
<endpoint address="" binding="webHttpBinding" contract="EReportService.IRestServiceImpl" behaviorConfiguration="web">
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding maxReceivedMessageSize="2097152" maxBufferSize="2097152" transferMode="Streamed"/>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<connectionStrings>
</connectionStrings>
</configuration>
When I run service I get the following exception:
The operation must have a single parameter whose type is Stream
If I do this:
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "UploadPhoto")]
Boolean UploadPhoto(Stream fileContents);
It works perfectly but I need to send more data with image.
This service is exposed for an Android tablet application. The following code show how I'm sending now images to server:
public static Boolean sendImage(String url, String filePath)
{
try
{
MultiValueMap<String, Object> formData;
Resource resource = new FileSystemResource(filePath);
// populate the data to post
formData = new LinkedMultiValueMap<String, Object>();
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<MultiValueMap<String, Object>> requestEntity =
new HttpEntity<MultiValueMap<String, Object>>(formData, requestHeaders);
GsonHttpMessageConverter messageConverter = new GsonHttpMessageConverter();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
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<Boolean> 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;
}
The error occurs when browsing to metadata endpoint (http://localhost:2351/RestServiceImpl.svc).
How can I send an image and parameters?