2014-02-10 77 views
0

我有我需要我的方法之一接受HTTP POST請求,另一個必須是非REST WCF服務。考慮下面的代碼:WCF REST方法和非REST方法

 [OperationContract] 
     long[] Send(string body, List<string> phoneNumbers); 


     [OperationContract] 
     [WebInvoke(Method="POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)] 
     long[] SendByPost(string body, string phoneNumbers); 

但我不知道爲什麼下面的引發錯誤:

合同「IService」的操作「發送」指定多個請求 車身參數沒有被序列化任何包裝元素。一個身體參數最多可以在沒有包裝元素的情況下被序列化。要麼 刪除額外的身體參數或將 WebGetAttribute/WebInvokeAttribute上的BodyStyle屬性設置爲Wrapped。

我怎樣才能解決這個問題呢?

+0

我認爲這個問題是'名單 phoneNumbers'。默認情況下,WebInvoke是一個'GET',但是'List '不能從URL反序列化。我想如果你把它改成'string [] phoneNumbers'應該可以工作。 – wdosanjos

+0

我的問題是,我不希望發送方法可以從URL和只有第二種方法必須啓用REST! –

+2

然後刪除'OperationContract'屬性。 – wdosanjos

回答

0

首先,要發送一個url中的多個參數,可以使用GET方法代替POST

[OperationContract] 
[WebInvoke(Method="POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)] 
long[] SendByGet(string body, string phoneNumbers); 

要麼把它變成這樣:

[OperationContract] 
[WebGet(UriTemplate = "/body={body}?phoneNumbers={phoneNumbers}"] 
long[] SendByPost(string body, string phoneNumbers); 

或者,如果你想使用POST

[OperationContract] 
[WebInvoke(Method="POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, 
UriTemplate = "MyUrl", RequestFormat = WebMessageFormat.Json 
, ResponseFormat = WebMessageFormat.Json)] 
long[] SendByPost(Info info); 

,這是你信息類:

[DataContract] 
Class Info 
{ 
    [DataMember] 
    public string Body {get; set;} 

    [DataMember] 
    public string PhoneNumbers{get; set;} 
} 
+0

這與他使用的HTTP方法無關。 – Oooogi

0

你有兩種方法,並要公開其中的一個休息和對方另一個結合,對不對?

對於這一點,你需要創建一個以上的接口,用於WCF服務([的ServiceContract])。

舉個例子來說,這個Web.config文件:

<system.serviceModel> 
<protocolMapping> 
    <add binding="basicHttpsBinding" scheme="https" /> 
</protocolMapping>  
<serviceHostingEnvironment minFreeMemoryPercentageToActivateService="1" aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" /> 
<bindings> 
    <webHttpBinding> 
    <binding name="RestBinding" maxBufferSize="1048576" maxReceivedMessageSize="1048576" maxBufferPoolSize="1048576"> 
     <readerQuotas maxDepth="2147483647" maxStringContentLength="1048576" maxArrayLength="1048576" maxBytesPerRead="1048576" maxNameTableCharCount="1048576"/> 
    </binding> 
    </webHttpBinding> 
    <basicHttpBinding> 
    <binding name="SoapBinding" maxReceivedMessageSize="20000000" messageEncoding="Text" maxBufferSize="20000000" maxBufferPoolSize="20000000" closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00"/> 
    </basicHttpBinding> 
    <wsHttpBinding> 
    <binding name="SecureHttpBinding" messageEncoding="Mtom" closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00" maxBufferPoolSize="20000000" maxReceivedMessageSize="20000000"> 
     <readerQuotas maxDepth="32" maxStringContentLength="20000000" maxArrayLength="20000000"/> 
     <security mode="None"> 
     <transport clientCredentialType="Windows"/> 
     </security> 
    </binding> 
    </wsHttpBinding> 
    <customBinding> 
    <binding name="RawReceiveCapable"> 
     <webMessageEncoding webContentTypeMapperType="WCFService.RawContentTypeMapper, WCFService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 
     <httpTransport manualAddressing="true" maxReceivedMessageSize="524288000" transferMode="Buffered" /><!--transferMode="Streamed"--> 
    </binding> 
    </customBinding> 
</bindings> 
<services> 
    <service name="WCFService.Service" behaviorConfiguration="WCFService.ServiceBehavior"> 
    <host> 
     <baseAddresses> 
     <add baseAddress="http://localhost:5000"/> 
     </baseAddresses> 
    </host> 
    <!-- Service Endpoints --> 
    <!-- Unless fully qualified, address is relative to base address supplied above --> 
    <endpoint address="SOAP" binding="basicHttpBinding" bindingConfiguration="SoapBinding" contract="WCFService.IService1" name="SOAP_XML"/> 
    <endpoint address="" binding="webHttpBinding" bindingConfiguration="RestBinding" contract="WCFService.IService2" behaviorConfiguration="EndpointJSONBehavior"/> 
    <endpoint address="MTOM" binding="wsHttpBinding" bindingConfiguration="SecureHttpBinding" contract="WCFService.IService1" name="SOAP_MTOM"/> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
    </service> 
</services> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="WCFService.ServiceBehavior"> 
     <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
     <serviceMetadata httpGetEnabled="true"/> 
     <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
     <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/> 
    </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
    <behavior name="EndpointXMLBehavior"> 
     <webHttp defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Xml"/> 
    </behavior> 
    <behavior name="EndpointJSONBehavior"> 
     <webHttp defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Json"/> 
    </behavior> 
    <behavior name="EndpointRawBehavior"> 
     <webHttp/> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 

你可以在這裏看到兩個ServiceContracts:IService1IService2。在IService1定義

要運行的方法,你需要使用肥皂封裝。 由IService2公開的方法可以用REST風格和簡單的方式進行調用,而不需要使用soap 封裝。

爲了您SendByPost()方法是基於REST的,你需要把它放在IService2並把其他方法在不同的ServiceContract(如IService1)。