2011-06-20 27 views
0

我試圖在一個屋檐下將SOAP和REST進行一些修改。但我不知道它是否是possbile。我的代碼如下,它只用於REST時工作,但因爲我試圖添加額外的Web服務作爲SOAP(使用配置),它不起作用。不知道如何配置它...以這種方式設置WCF服務作爲REST和SOAP是否可能?

我有接口:

[ServiceContract] 
public interface IVLSContentServiceREST 
{ 
    [OperationContract] 
    [WebGet] 
    string EchoWithGet(string s); 

    [OperationContract] 
    [WebInvoke] 
    string EchoWithPost(string s); 

} 

[ServiceContract] 
public interface IVLSContentServiceSOAP 
{ 
    [OperationContract] 
    [WebGet] 
    string EchoWithGet(string s); 

    [OperationContract] 
    [WebInvoke] 
    string EchoWithPost(string s); 
} 

然後,我有一個名爲VLSContentService.svc與此文件:

​​

和CS(代碼隱藏)文件:

public class VLSContentService : IVLSContentServiceSOAP, IVLSContentServiceREST 
{ 

    string IVLSContentServiceSOAP.EchoWithGet(string s) 
    { 
     return "You said " + s; 
    } 

    string IVLSContentServiceSOAP.EchoWithPost(string s) 
    { 
     return "You said " + s; 
    } 


    string IVLSContentServiceREST.EchoWithGet(string s) 
    { 
     return "You said " + s; 
    } 

    string IVLSContentServiceREST.EchoWithPost(string s) 
    { 
     return "You said " + s; 
    } 

} 

而且配置:

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
     <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 

    <system.serviceModel> 

    <!---Add the service--> 
    <services> 
     <service behaviorConfiguration="VLSContentServiceBehaviour" name="VLSContentService"> 
     <endpoint address="rest" behaviorConfiguration="VLSContentServiceEndpointBehaviour" binding="webHttpBinding" contract="IVLSContentServiceREST"/> 
     <endpoint address="soap" behaviorConfiguration="VLSContentServiceEndpointBehaviour" binding="basicHttpBinding" contract="IVLSContentServiceSOAP"/> 
     </service> 
    </services> 

    <!---Add the behaviours--> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="VLSContentServiceBehaviour"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     </serviceBehaviors> 


     <!---Add the behaviours--> 
     <endpointBehaviors> 
     <behavior name="VLSContentServiceEndpointBehaviour"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 

    </system.serviceModel> 
</configuration> 
+0

是的,它是可能的,我做了這樣承載同一個合同的兩個端點。我稍後會發布我的解決方案。 –

回答

3

你不需要合同的兩個版本 - 只是用不同的綁定

<services> 
    <service behaviorConfiguration="VLSContentServiceBehaviour" name="VLSContentService"> 
    <endpoint address="rest" behaviorConfiguration="VLSContentServiceEndpointBehaviour" binding="webHttpBinding" contract="IVLSContentService"/> 
    <endpoint address="soap" behaviorConfiguration="VLSContentServiceEndpointBehaviour" binding="basicHttpBinding" contract="IVLSContentService"/> 
    </service> 
</services> 
相關問題