对于操作中的请求,UploadPhotoStream是一个流,操作必须有一个类型为Stream的参数

时间:2013-03-19 09:19:48

标签: c# wcf rest soap

我一直在搜索这个问题,但仍无法找到确切的解决方案。

代码:

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);
    }
}  

配置:

<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>  

当我尝试运行http://localhost:64039/Merchant/Merchant.svc时 它显示错误:

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

我不知道该怎么做。

3 个答案:

答案 0 :(得分:5)

对于未来的观看者,如果您想实现这个目标,您可以将多个参数与Stream放在一起,那么您需要做的是避免使用SVC文件来创建Rest或肥皂服务。而是将此添加到您的服务项目Global.asax文件,make路由表中,例如:

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

在您的网络服务中:

[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);
}  

在web.config中:

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

并致电:

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

希望它有所帮助。

答案 1 :(得分:4)

您需要在操作中使用一个参数来接收包含所需属性的对象。

请参阅下面的代码:

 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]
    Stream FileData { get; set; }
}

答案 2 :(得分:0)

您应该在web.config中按以下方式配置服务。然后,您就可以使用更多URL参数发送Stream。 Stream参数应该是函数中的最后一个parmater。

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="YourServiceClassWithNamespace">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJSON" contract="YourServiceInterfaceWithNamespace" behaviorConfiguration="EndpointBehavior" />
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingWithJSON" crossDomainScriptAccessEnabled="true" maxReceivedMessageSize="67108864"
             openTimeout="00:10:00" closeTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00">
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="EndpointBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>