2

I have been trying fruitlessly to try and create a simple REST file upload WCF Service, which accepts more than one piece of information.

I have found a fair few places on the Internet that suggest that it is possible to have more than one parameter to a OperationContract that has a Stream as one of the parameters (Upload file with REST, Using Silverlight and WCF to create a RESTful File Upload Utility, WCF REST File Upload, Uploading File to server from ASP.Net client using WCF REST Service, etc.) but no matter how many times I try I always get the same error message.

For request in operation Upload to be a stream the operation must have a single parameter whose type is Stream.

Is it actually possible to have an OperationContract that accepts more than one parameter when one of them is a Stream? If so are there any particular steps that need to be taken which I may have missed that would have caused for me not to be able to do so.

For reference I am using Visual Studio 2010, WCF 4.0

I have uploaded the example project that I am trying to get to work, its literally the minimum of what going by the examples I have read that I should need to be able to upload a file with additional parameters. My Example.

Community
  • 1
  • 1
Sam Jenkins
  • 1,284
  • 1
  • 12
  • 30
  • 1
    Where do you get the error message? When trying to browse to the "service help page"? There's a known bug in WCF (IIRC at least until 4.0) which the service help page says that there is an error, but the operation works out just fine. Try calling your operation to see if this will work. – carlosfigueira Sep 01 '11 at 02:14
  • I get the error message when I am trying to view the service (by going to http://localhost:1752/Service1.svc) or when I try to create a service reference in the client application. – Sam Jenkins Sep 01 '11 at 06:36
  • There's no support for "Add Service Reference" for REST endpoints - so you're hitting that issue I talked about. Try copying the service interface to the client, then using the `WebChannelFactory` class to consume the service. Or you can use `WebClient`, which is often simple enough. – carlosfigueira Sep 01 '11 at 14:11

2 Answers2

4

Yes, it is possible. I am doing it with UriTemplates.

[WebGet(UriTemplate="ReceiveChunk/{complete}?offset={offset}", Method ="POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
public string ReceiveChunk(string complete, int offset, Stream fileContents)
{
//implementation
}

Is that what you're looking for?

user888382
  • 113
  • 1
  • 2
  • 5
  • but still you will get exception like: InvalidOperationException: For request in operation A to be a stream the operation must have a single parameter. – Rajiv yadav Apr 24 '12 at 10:48
0

I know this is an old question but I've been struggling with this today. I finally found this link, specifically:

2. You are not using WebEndpoint or WebServiceHost;

I was self hosting my service in a console app and was using ServiceHost not WebServiceHost. Changing the host type resolved my issue and explained my confusion. SOAP WCF services require the operation to have a single Stream parameter only, REST WCF services don't.

David Waterworth
  • 2,214
  • 1
  • 21
  • 41