2013-06-28 45 views
1

目前我們使用.NET 3.5和WSCF.blue編寫合同優先的WCF SOAP服務。 這使我們能夠設計使用Xsd文件交換的Xml文檔。是否有可能首先執行WCF合約並公開REST/JSON和SOAP/XML設定點?

現在,WSDL 2.0存在,你可以設計合同REST端點並沒有先在.NET 4.5合同適當的支持下,我們有以下問題:

是否有可能升級到Visual Studio 2012,保持我們現有的Xsd集並自動公開REST和/或SOAP端點?

是否可以升級到Visual Studio 2012,保留我們現有的Xsd集並自動交換Xml和/或Json文檔?

回答

0

這裏是我的解決方案:

您至少需要Visual Studio 2012。

創建一個WCF服務庫項目。

包含您的Xsd文件以自動創建數據合同包裝。

寫下您的ServiceContract和階級是這樣的:

using System; 
using System.Linq; 
using System.Collections.Generic; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 

namespace RestSoapTest 
{ 
    public class Service1 : IService1 
    { 
     public List<CompositeType> GetData(string paramA, string paramB) 
     { 
      List<CompositeType> output = new List<CompositeType>(); 

      output.Add(new CompositeType() 
      { 
       Key = paramA, 
       Value = paramB, 
      }); 
      output.Add(new CompositeType() 
      { 
       Key = paramA, 
       Value = paramB, 
      }); 
      output.Add(new CompositeType() 
      { 
       Key = paramA, 
       Value = paramB, 
      }); 

      return output; 
     } 

     public void PutData(string paramA, string paramB, List<CompositeType> data) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    [ServiceContract] 
    public interface IService1 
    { 
     [OperationContract] 
     [WebGet(UriTemplate = "GetData/{paramA}/{paramB}")] 
     List<CompositeType> GetData(string paramA, string paramB); 

     [OperationContract] 
     [WebInvoke(UriTemplate = "PutData/{paramA}/{paramB}")] 
     void PutData(string paramA, string paramB, List<CompositeType> data); 
    } 

    [DataContract] 
    public class CompositeType 
    { 
     [DataMember] 
     public string Key { get; set; } 
     [DataMember] 
     public string Value { get; set; } 
    } 
} 

(無論是手工編寫你的DataContract或與XSD控制它)

添加一個虛擬主機項目,引用WCF項目,並添加此web.config文件:

<?xml version="1.0"?> 
<configuration> 
    <system.serviceModel> 
    <standardEndpoints> 
     <webHttpEndpoint> 
     <standardEndpoint name="standardRest" automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" helpEnabled="true"/> 
     </webHttpEndpoint> 
     <mexEndpoint> 
     <standardEndpoint name="standardMex"/> 
     </mexEndpoint> 
    </standardEndpoints> 
    <services> 
     <service name="RestSoapTest.Service1"> 
     <endpoint name="rest" address="" kind="webHttpEndpoint" endpointConfiguration="standardRest" contract="RestSoapTest.IService1"/> 
     <endpoint name="mex" address="mex" kind="mexEndpoint" endpointConfiguration="standardMex" contract="RestSoapTest.IService1"/> 
     <endpoint name="soap" address="soap" binding="basicHttpBinding" contract="RestSoapTest.IService1"/> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="true"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment> 
     <serviceActivations> 
     <add relativeAddress="RestSoapTest.svc" service="RestSoapTest.Service1"/> 
     </serviceActivations> 
    </serviceHostingEnvironment> 
    </system.serviceModel> 
    <system.web> 
    <compilation debug="true"/> 
    </system.web> 
</configuration> 

現在你可以在瀏覽器到:

/RestSoapTest.svc = SOAP端點/幫助頁面

/RestSoapTest.svc?wsdl = SOAP元

/RestSoapTest.svc/help =自動其他幫助頁面

/RestSoapTest.svc/url/到/方法=執行REST動作

對於獲取方法,使用WebGet和確保UriTemplate和方法的原型匹配的參數個數,否則你會得到一個錯誤

對於put方法,使用WebInvoke並確保UriTemplate中的參數計數等於(原型中的計數+ 1)。

如果這樣做,任何單個未映射的參數將被WCF框架假定爲通過HTTP Post的Request主體進入。

如果您需要通過正文進入多個參數,則需要將參數格式設置爲Wrapped,它默認爲Bare。包裝自動包裝參數名稱/值屬性中的多個參數。

相關問題