2013-03-19 53 views
4

我一直在搜索這個問題,但仍然無法找到確切的解決方案。對於請求在操作UploadPhotoStream是一個流操作必須有一個參數的類型是流

代碼:

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. 

我沒有任何想法該怎麼辦。

+0

錯誤信息的哪部分你不明白嗎? – 2013-03-19 09:22:37

+0

@JohnSaunders我不明白爲什麼它不接受多個參數。因爲我看到一個具有這種UriTemplate的項目,它接受包括流在內的多個參數。我只是做了相同的配置和格式,除了我的wcf使用'svc'文件,而我看到的項目使用WCF 4.0 REST模板,它沒有'svc'文件。 – fiberOptics 2013-03-20 01:38:35

+0

你說這兩種項目類型之間沒有區別 - 除了差異。機會是,這種差異說明了行爲上的差異。 – 2013-03-20 01:39:29

回答

4

對於未來的觀衆,如果要實現這個目標,其中你將能夠把多個參數與Stream在一起,你需要做的是避免使用SVC文件創建Rest或Soap服務。相反,添加此對您的服務項目,Global.asax文件中,建立路由表,例如像:

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

在Web服務:

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

希望它有幫助。

+0

無法添加「RouteTable.Routes.Add(新的ServiceRoute(」MyApp/rest/Photo「,新的WebServiceHostFactory(),typeof(PhotoComponent)));」我需要解決方案。 – 2014-01-06 11:54:29

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; } 
} 
+0

非常感謝。我以前見過這個解決方案。如果我不能不使用課程來完成任務,我會採用這種方法。 – fiberOptics 2013-03-20 01:41:03

+0

我無法理解你的解決方案!我想讓它成爲一個POST請求,我需要有它的文件名。 – 2014-01-06 11:56:39

+0

@Chris Holwerda,謝謝你的簡短邏輯。 – 2017-11-29 01:09:03

0

您應該在web.config中按以下方式配置服務。然後你就可以發送一個包含更多URL參數的Stream。 Stream參數應該是函數中的最後一個參數。

<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> 
相關問題