2012-11-06 88 views
1

我有一個WCF SOAP服務約100功能。我只想將其中的一部分公開給REST端點。只有一份合同可以做到這一點嗎?WCF服務公開一些功能作爲REST

我加了一個休息的行爲和REST端點是這樣的:

<behaviors> 
     <endpointBehaviors> 
     <behavior name="RestBehavior"> 
      <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 


<endpoint address="json" binding="webHttpBinding" behaviorConfiguration ="RestBehavior" contract="Test.IService" /> 
<endpoint address="" binding="customBinding" bindingConfiguration="BufferedHttpBinaryBusiness" 
      bindingName="BufferedHttpBinaryBusiness" contract="Test.IService" /> 

,並添加WebGet屬性我要揭露在休息端點的功能:

<WebGet(BodyStyle:=WebMessageBodyStyle.Wrapped, RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json, UriTemplate:="/IsRegistered?Id={Id}")> 
    <ServiceModel.OperationContract()> 
    Function IsRegistered(ByVal Id As String) As Boolean 

但我也有我的其他我不想公開爲REST的函數。

<ServiceModel.OperationContract()> 
Function GetName(ByVal x As Long, ByVal y As String) As String 
<ServiceModel.OperationContract()> 
Function GetId(ByVal name As String) As String 

我得到的錯誤是:

System.InvalidOperationException: Operation 'GetName' of contract 'IService' specifies multiple request body parameters to be serialized without any wrapper elements. At most one body parameter can be serialized without wrapper elements. Either remove the extra body parameters or set the BodyStyle property on the WebGetAttribute/WebInvokeAttribute to Wrapped. 
    at System.ServiceModel.Description.WebHttpBehavior.TryGetNonMessageParameterType(MessageDescription message, OperationDescription declaringOperation, Boolean isRequest, Type& type) 
    at System.ServiceModel.Description.WebHttpBehavior.ValidateBodyStyle(OperationDescription operation, Boolean request) 
    at System.ServiceModel.Description.WebHttpBehavior.<>c__DisplayClass10.<>c__DisplayClass13.<GetRequestDispatchFormatter>b__d() 
    at System.ServiceModel.Description.WebHttpBehavior.<>c__DisplayClass10.<GetRequestDispatchFormatter>b__c() 
    at System.ServiceModel.Description.WebHttpBehavior.HideReplyMessage(OperationDescription operationDescription, Effect effect) 
    at System.ServiceModel.Description.WebHttpBehavior.GetRequestDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint) 
    at System.ServiceModel.Description.WebHttpBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) 
    at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) 
    at System.ServiceModel.ServiceHostBase.InitializeRuntime() 
    at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) 
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) 
    at Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info) 

當我添加WebGet屬性此功能,則發生了另一香皂的功能相同的錯誤。它似乎需要我將WebGet屬性應用於所有函數。換句話說,它要求我公開合約中的所有函數以暴露給REST端點。但我只希望其中的一些能夠作爲REST進行擴展。不可能嗎?

回答

4

總之,沒有。您必須擁有兩個不同的合同才能同時使用SOAP和REST。儘管如此,您可以在同一個配置中添加兩個端點。好處是您只需要在單獨的界面/合同中定義REST操作,並且您也可以從該界面繼承服務,並且不必更改代碼。

下面的文章是一個偉大的交代,和方式爲您服務,實現兩者。我在我寫的兩個自己的服務中用這個例子。

How to enable REST and SOAP both on the same WCF service