none
The operation must have a single parameter whose type is Stream

    Question

  • I'm developing a WCF .NET Framework 4.0 with C#.

    I need to send an image with two or three parameters. This is OperationContract:

    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "UploadPhoto/{eReportId}/{imageType}")]
    Boolean UploadPhoto(Stream fileContents, string eReportId, string imageType);

    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.

    How can I do this?

    Wednesday, November 28, 2012 9:16 AM

All replies

  • You can use MessageContract, like below

     [MessageContract]

    public class FileInfo 
    {
        [MessageHeader(MustUnderstand = true)]
        public string FileName;

        [MessageHeader(MustUnderstand = true)]
        public string Param1;

        [MessageHeader(MustUnderstand = true)]
         public string Param2;

        [MessageBodyMember(Order = 1)]
        public Byte[] FileByte;

    }

    and you can pass it in OperationContract as input-

    [OperationContract]
    void UploadFile(FileInfo file);



    Harpal Singh

    Wednesday, November 28, 2012 7:44 PM
  • Hi,

    Add what Harpal suggested, if this is a REST endpoint, this programming model is actually supported because REST has its own formatter which handles multiple parameters correctly. However, if you continue to see this issue, then please make sure your WebHttpBehavior is set up correctly. Possible reason for not having that behavior set up correctly could be:

    1. You are using a custom behavior, and its endpoint does not have the correct the contract name matching with the WCF contract;

    2. You are not using WebEndpoint or WebServiceHost;

    How to solve System.InvalidOperationException: For request in operation Test to be a stream the operation must have a single parameter whose type is Stream

    http://blogs.msdn.com/b/hongmeig/archive/2010/03/02/how-to-solve-system-invalidoperationexception-for-request-in-operation-test-to-be-a-stream-the-operation-must-have-a-single-parameter-whose-type-is-stream.aspx


    Leo Tang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, November 29, 2012 1:43 AM
    Moderator
  • Hi,

    Please also change WebMessageBodyStyle.Bare to WebMessageBodyStyle.Wrapped to allow multiple parameters.


    Leo Tang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, November 29, 2012 6:55 AM
    Moderator
  • I have used this Visual Studio template to create my WCF service:

    WebMessageBodyStyle.Wrapped doesn't help me to solve the problem, I get the same execption.

    This is my Web.config:

    <configuration>
    
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
      <system.serviceModel>
        <services>
          <service name="WcfImageService.ImageService" behaviorConfiguration="ServiceBehaviour">
            <endpoint address="" binding="webHttpBinding" contract="WcfImageService.IImageService" behaviorConfiguration="web"/>
          </service>
        </services>
        <bindings>
          <webHttpBinding>
            <binding maxReceivedMessageSize="2097152" maxBufferSize="2097152" transferMode="Streamed"/>
          </webHttpBinding>
        </bindings>
        <behaviors>
          <endpointBehaviors>
            <behavior name="web">
              <webHttp />
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="ServiceBehaviour">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
            <behavior>
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
     <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
      </system.webServer>
      
    </configuration>
    Any ideas?

    Sunday, December 02, 2012 5:59 PM