2017-01-16 67 views
0

我試圖從WCF服務的URL使用下面的代碼來獲得JSON:轉換WCF休息

[WebInvoke(Method = "GET", 
      RequestFormat = WebMessageFormat.Json, 
      ResponseFormat = WebMessageFormat.Json, 
      UriTemplate = "players")] 
public List<Person> GetResult() 
{ 
    List<Person> results = new List<Person>(); 
    results.Add(new Person("Peyton", "Manning", 35)); 
    results.Add(new Person("Drew", "Brees", 31)); 
    results.Add(new Person("Tony", "Romo", 29)); 
    return results; 
} 

web.config

<?xml version="1.0"?> 
<configuration> 
    <appSettings> 
     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/> 
    </appSettings> 
    <system.web> 
     <compilation debug="true" targetFramework="4.5.2"/> 
     <httpRuntime targetFramework="4.5.2"/> 
     <httpModules> 
      <add name="ApplicationInsightsWebTracking" 
       type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/> 
     </httpModules> 
    </system.web> 
    <system.serviceModel> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior> 
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
        <serviceDebug includeExceptionDetailInFaults="false"/> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     <protocolMapping> 
      <add binding="basicHttpsBinding" scheme="https"/> 
     </protocolMapping>  
     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
            multipleSiteBindingsEnabled="true"/> 
    </system.serviceModel> 
    <system.webServer> 
    ------ 
    </system.webServer> 
</configuration> 

但是當我開始調試WCF服務,瀏覽到

http://localhost:55636/Service1.svc/players 

我得到一個空白頁 - 爲什麼?我該怎麼做,也許我需要轉換WCF休息?

+1

您使用的是哪種瀏覽器? IE例如開始下載json文件並顯示空白頁面。你能不能發佈你的web.config? – MadOX

+0

Firefox和Chrome返回空白頁,我沒有測試IE –

+1

你有沒有檢查過檢查員?特別是在回覆標籤中,你回來了什麼? –

回答

3

要在瀏覽器使用WCF你需要定義webHttpBinding

<services> 
    <service name="yor_name"> 
    <endpoint address="" binding="webHttpBinding" behaviorConfiguration="webBehavior" contract="yourservice_Interface"> 
    </endpoint> 
    </service> 
</services> 

WebConfig

<endpointBehaviors> 
    <behavior name="webBehavior"> 
     <webHttp /> 
    </behavior> 
    </endpointBehaviors> 

有關配置的詳細信息,請遵循:

https://www.codeproject.com/Tips/803009/Configure-WCF-Service-to-REST

或這裏:

https://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide

+0

我的web.config沒有服務標籤,你想讓我添加它嗎? –

+2

那你怎麼配置你的端點呢? WCF = A + B + C(地址,綁定,合同)。 Web.config將這些東西放在一起。 – MadOX

+0

我更新了這個問題,其實我沒有改變web.config上的任何東西,你能不能更新我的web.config –