WCF服務中如何同時啟用SOAP與REST兩種類型的服務端點

在WCF服務中,除了原先SOAP的服務外,我們也可以建立REST的服務類型,那如果說要在同一支服務當中,同時建立兩種的話應該要怎麼做呢?這邊先簡單筆記一下,之後有時間再來補齊

在WCF服務中,除了原先SOAP的服務外,我們也可以建立REST的服務類型,那如果說要在同一支服務當中,同時建立兩種的話應該要怎麼做呢?這邊先簡單筆記一下,之後有時間再來補齊

主要的方式就是要建立兩個EndPoint,一個給SOAP用,一個給REST用;而Interface的部分也要建立兩份,比如說REST的介面像是下面這樣

 

    [ServiceContract]
    public interface IRestfulService
    {
        #region Member query
 
        [OperationContract()]
        [WebGet(UriTemplate = "/Members/Query/MemberId/{memberId}"ResponseFormat = WebMessageFormat.JsonBodyStyle = WebMessageBodyStyle.Wrapped)]
        Data.Member restGetMemberByMemberId(string memberId);
 
        [OperationContract()]
        [WebGet(UriTemplate = "/Members/Query/CardUid/{cardUid}"ResponseFormat = WebMessageFormat.Json)]
        Data.Member restGetMemberByCardUid(string cardUid);
 
        [OperationContract()]
        [WebGet(UriTemplate = "/Members/Query/Department/{departmentId}"ResponseFormat = WebMessageFormat.Json)]
        List<Data.Member> restGetMembersByDept(string departmentId);
 
        #endregion
 
    }

而SOAP的比如說是下面這樣

    [ServiceContract()]
    interface ISoapService
    {
        [OperationContract()]
        Data.Member GetMemberByMemberId(string memberId);
 
        [OperationContract()]
        Data.Member GetMemberByCardUid(string cardUid);
 
        [OperationContract()]
        List<Data.Member> GetMembersByDept(string departmentId);
    }

HOST這邊就要同時實作兩個介面,例如

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class YourServiceNameIRestfulServiceISoapService
    {
 
        public Data.Member restGetMemberByMemberId(string memberId)
        {
            Data.Member result = new Data.Member();
            result.cardUid = "12345678";
            result.expStart = DateTime.Now;
            result.expEnd = DateTime.Now.AddYears(1);
            return result;
        }
 
        public Data.Member restGetMemberByCardUid(string cardUid)
        {
            throw new NotImplementedException();
        }
 
        public List<Data.Member> restGetMembersByDept(string departmentId)
        {
            throw new NotImplementedException();
        }
 
        #region Soap interface implement
 
        public Data.Member GetMemberByMemberId(string memberId)
        {
            Data.Member result = new Data.Member();
            result.cardUid = "12345678";
            result.expStart = DateTime.Now;
            result.expEnd = DateTime.Now.AddYears(1);
            return result;
        }
 
        public Data.Member GetMemberByCardUid(string cardUid)
        {
            throw new NotImplementedException();
        }
 
        public List<Data.Member> GetMembersByDept(string departmentId)
        {
            throw new NotImplementedException();
        }
 
        #endregion
        
    }

而重點的web.config大致會像是下面這樣

xml version="1.0"?>
<configuration>
 
    <system.web>
        <compilation debug="truetargetFramework="4.0" />
    </system.web>
 
    <system.serviceModel>
        <bindings>
            <webHttpBinding>
                <binding name="webHttpBindingCfgmaxBufferSize="8192000"
                         maxReceivedMessageSize="8192000">
                    <security mode="Transport">
                        <transport clientCredentialType="Basic"/>
                    </security>
                </binding>
            </webHttpBinding>    
        </bindings>
        
        <behaviors>
            <serviceBehaviors>
                <behavior>
                    
                    <serviceMetadata httpGetEnabled="true"/>
                    
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                </behavior>
            </serviceBehaviors>
            
            <endpointBehaviors>
                <behavior name="RestEndPointCfg">
                    <webHttp helpEnabled="trueautomaticFormatSelectionEnabled="true"/>
                </behavior>
            </endpointBehaviors>
 
        </behaviors>
 
        <services>
            <service name="rootNameSpace.YourServiceName">                <endpoint address="restbehaviorConfiguration="RestEndPointCfg"
                    binding="webHttpBindingname="RestEndPointcontract="rootNameSpace.IRestfulService" />
                <endpoint address="" binding="basicHttpBindingname="SoapEndPoint"
                    contract="rootNameSpace.ISoapService" />
                <endpoint address="mexbinding="mexHttpBindingcontract="IMetadataExchange" />
            </service>
        </services>
 
        
        
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"></serviceHostingEnvironment>
    </system.serviceModel>
    
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        
        <directoryBrowse enabled="false"/>
    </system.webServer>
 
</configuration>