2012-12-07 67 views
0

我建立了一個WCF REST Web服務(WCF服務應用程序),當我與Visual Studio 2012調試,它會旋轉起來WCF測試客戶端應用程序,並嘗試添加我的網站服務。我收到以下錯誤:VS2012 WCF REST服務 - 錯誤:無法獲取元數據

Error: Cannot obtain Metadata from http://localhost:50925/Service1.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:50925/Service1.svc Metadata contains a reference that cannot be resolved: 'http://localhost:50925/Service1.svc'. Content Type application/soap+xml; charset=utf-8 was not supported by service 

我訪問上面鏈接的MSDN文檔,我相信我已正確設置我的web.config。

Web.config文件:

<?xml version="1.0"?> 
<configuration> 
<system.web> 
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" /> 
</system.web> 
<system.serviceModel> 
<services> 
    <service name="Service1" behaviorConfiguration="Service1Behavior">  
    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex">  </endpoint> 
    </service> 
</services> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="Service1Behavior"> 
     <serviceMetadata httpGetEnabled="true"/>  
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
</system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
</system.webServer> 
</configuration> 

我還是繼續收到同樣的錯誤消息。我是新來的WCF/VS 2012和.NET 4.0,但我在VS2008的.Net 2.0很流暢。

回答

4

WCF REST服務不公開元數據,這就是爲什麼你都拿到那個消息(見this blog post更多細節)。如果您正在使用WCF測試客戶端,它正在與SOAP服務(端點)進行通信,而不是REST服務。

如果要啓用一個RESTful端點,可以定義配置中的服務元素,並確定它內部的一個端點,它使用webHttpBinding,和端點也需要一個behaviorConfiguration指向與<webHttp/>行爲的終結點行爲。

0

事實證明我創建了一個新的WCF項目,看着什麼怎麼回事。 VS2012/.Net 4.0不喜歡定義端點的服務配置。這是我的新web.config工作正常。我需要研究這是爲什麼,但至少它回答了我的問題。

<?xml version="1.0"?> 
<configuration> 
<system.web> 
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" /> 
</system.web> 
<system.serviceModel> 
<behaviors> 
    <serviceBehaviors> 
    <behavior> 
     <serviceMetadata httpGetEnabled="true"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
</system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
</system.webServer> 
</configuration>