Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rest

c#

soap

wcf

I've been searching this issue but still unable to find exact solution.

Code:

namespace StackSample.Logic
{
    [ServiceHeaderBehavior]
    [MerchantHeaderBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Merchant : Interfaces.IMerchant
    {
        public bool UploadPhotoStream(string productid, string photoid, Stream fileData)
        {
            Logic.Components.Product ca = new Logic.Components.Product();
            return ca.UploadPhotoStream(Common.UserValues().Merchant, productid, photoid, fileData);
        }
    }
}

namespace StackSample.Interfaces
{
    [ServiceContract]
    public interface IMerchant
    {
        [OperationContract]
        [WebInvoke(UriTemplate = "UploadPhotoStream?productid={productid}&photoid={photoid}", Method = "POST")]
        bool UploadPhotoStream(string productid, string photoid, Stream fileData);
    }
}  

Config:

<bindings>
  <basicHttpBinding>
    <binding name="SOAPSecure">
      <security mode="None" />
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="2097152" maxBytesPerRead="4096" maxNameTableCharCount="2097152" />
    </binding>
    <binding name="SOAPSecureTransfer" transferMode="Streamed" closeTimeout="00:05:00" openTimeout="00:05:00" receiveTimeout="00:15:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Mtom" textEncoding="utf-8" useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="2147483647" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
      </security>
    </binding>
  </basicHttpBinding>
  <webHttpBinding>
    <binding name="RESTSecure">
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
      </security>
    </binding>
    <binding name="RESTSecureTransfer" transferMode="Streamed" closeTimeout="00:05:00" openTimeout="00:05:00" receiveTimeout="00:15:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="2147483647" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
      </security>
    </binding>
  </webHttpBinding>
</bindings>
<!-- behaviors -->
<behaviors>
  <endpointBehaviors>
    <behavior name="JSON">
      <webHttp defaultOutgoingResponseFormat="Json" />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="Default">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />


<services>  
  <service behaviorConfiguration="Default" name="StackSample.Logic.Merchant">
    <endpoint address="soap" binding="basicHttpBinding" bindingConfiguration="SOAPSecureTransfer" contract="StackSample.Interfaces.IMerchant" />
    <endpoint address="rest" behaviorConfiguration="JSON" binding="webHttpBinding" bindingConfiguration="RESTSecureTransfer" contract="StackSample.Interfaces.IMerchant" />
  </service>
</services>  

When I try to run http://localhost:64039/Merchant/Merchant.svc
It shows an error:

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

I don't have any idea on what to do.

like image 704
fiberOptics Avatar asked Mar 19 '13 09:03

fiberOptics


2 Answers

For future viewers, if you want to achieve this goal, wherein you will be able to put multiple parameters together with Stream, what you need to do is to avoid using SVC files to create Rest or Soap service. Instead add this on your services project, Global.asax file, the make route table, like for example:

public class Global : System.Web.HttpApplication
{
    RouteTable.Routes.Add(new ServiceRoute("MyApp/rest/Photo", new WebServiceHostFactory(), typeof(PhotoComponent)));
}  

In your web service:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class PhotoComponent : Interfaces.IPhotoComponent
{
    public bool UploadPhotoStream(string productid, string photoid, Stream fileData)
    {
        // some code....
    }
}

[ServiceContract]
public interface PhotoComponent
{
    [OperationContract]
    [WebInvoke(UriTemplate = "UploadPhotoStream/{productid}/{photoid}", Method = "POST")]
    bool UploadPhotoStream(string productid, string photoid, System.IO.Stream fileData);
}  

inside web.config:

<services>  
  <service behaviorConfiguration="Default" name="StackSample.Logic.PhotoComponent">
    <endpoint behaviorConfiguration="JSON" binding="webHttpBinding" bindingConfiguration="RESTSecureTransfer" contract="StackSample.Interfaces.PhotoComponent" />
  </service>
</services>   

and call it:

https://127.0.0.1/MyApp/rest/Photo/UploadPhotoStream/{productid}/{photoid}  

Hope it helps.

like image 121
fiberOptics Avatar answered Oct 03 '22 14:10

fiberOptics


You need to have a single parameter in your operation that takes in an object that contains properties that you need.

See code below for example:

public interface IMerchant
{
    bool UploadPhotoStream(UploadData request);
}

public class YourService : IMerchant
{
    public bool UploadPhotoStream(UploadData request)
    {
        throw new NotImplementedException();
    }
}

[DataContract]
public class UploadData
{
    [DataMember]
    string ProductId { get; set; }

    [DataMember]
    string PhotoId { get; set; }

    [DataMember]
    System.IO.Stream FileData { get; set; }
}
like image 32
Chris Holwerda Avatar answered Oct 03 '22 14:10

Chris Holwerda