2017-01-16 147 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 –