2013-04-16 97 views
1

我有這樣的代碼我的WCF界面中:WCF:通過WebGet獲取WCF的返回值屬性

[OperationContract] 
     [WebGet(UriTemplate = "/GetData/", ResponseFormat = WebMessageFormat.Json)] 
     string GetData(); 

而且這裏面我的服務頁面:

public string GetData() 
{ 
    return "Please help"; 
} 

我想要得到的的GetData函數的值時,我把它稱爲URL:

http://localhost: 12345/Services/MyWCF.svc/GetData/ 

進入該網址後,我應該能夠下載結果由於JSON格式的記事本內部。 我不確定我的WCF出了什麼問題。

請幫忙!

請讓我知道如果我沒有讓我的問題清楚。

在此先感謝。

+1

你是如何託管你的服務?你確定它正在運行?當您訪問該網站時是否顯示任何錯誤? – Thelonias

+0

奇怪的是,我的WCF在我的瀏覽器中運行,但我沒有注意到我的服務名稱錯誤。我只是重命名它,它的工作!無論如何,感謝您查看我的問題;) – SyntaxError

回答

0

必須使用特定的綁定&一個web http行爲。 假設我的服務就是WebApplication1.Services.MyWCF下的配置應該是:

<system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
      <behavior name="MyServiceBehavior"> 
       <serviceMetadata httpGetEnabled="true" /> 
       <serviceDebug includeExceptionDetailInFaults="true"/> 
      </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
      <behavior name="WebBehavior"> 
       <webHttp /> 
      </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <services> 
     <service behaviorConfiguration="MyServiceBehavior" name="WebApplication1.Services.MyWCF"> 
      <endpoint address="" binding="webHttpBinding" behaviorConfiguration="WebBehavior" contract="WebApplication1.Services.IMyWCF"/> 

     </service> 
    </services> 
</system.serviceModel> 

如果你是.NET 4.0下,協議映射也將幫助你很多。看here

+0

我能夠通過重命名我的服務來解決我的問題。我能夠運行WCF,但沒有注意到我的服務名稱錯誤。任何方式,感謝您的解決方案。我相信這也應該起作用。 ;) – SyntaxError