WCF Rest Service仅将硬编码数据返回给Web浏览器

时间:2016-01-21 19:35:33

标签: c# .net web-services wcf rest

我正在尝试设置一个wcf休息服务,该服务只返回恐龙实体的集合,以便在网页上显示它们。该服务的问题是,我可以调用它的方法并接收硬编码的测试数据以及来自我的数据库的数据,以便控制台程序测试目的很好,但当我尝试在浏览器中查看服务时,我只能接收硬编码的测试数据,但不是数据库。当我从数据库中获取数据时,该服务返回一个错误的请求错误。当我将数据硬编码到服务中时,我得到了一个很好的json对象。

这是我与Entity Framework一起用来从sql数据库收集数据的模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice.Models
{
    public class Dino
    {
        public int DinoID { get; set; }
        public string Name { get; set; }

        public int Health { get; set; }

        public int Stamina { get; set; }

        public int Oxigen { get; set; }

        public int Weight { get; set; }

        public int Damage { get; set; }
    }
}

然后映射基类属性并将其传送到服务返回的DTO对象:

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

namespace Practice.Services.DataTransferObjects
{
    [DataContract(Name = "DTODino")]
    public class DTODino
    {
        [DataMember]
        public int DinoID { get; set; }
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public int Health { get; set; }
        [DataMember]
        public int Stamina { get; set; }
        [DataMember]
        public int Oxigen { get; set; }
        [DataMember]
        public int Weight { get; set; }
        [DataMember]
        public int Damage { get; set; }
    }
}

我使用Automapper通过调用ConvertToDTODinoCollection方法在服务实现本身上执行此操作,该方法如下所示:

private DTODinoCollection ConvertToDTODinoCollection(DinoCollection tempList)
    {
        DTODinoCollection returnList = new DTODinoCollection();
        foreach (Dino d in tempList)
        {
            DTODino tempDino = new DTODino();
            tempDino = Mapper.Map<DTODino>(d);

            returnList.Add(tempDino);
        }
        return returnList;
    }

这是我的服务合同:

    [OperationContract]
    [Description("Gets a collection of dino records from the database.")]
    [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "Dino/Collection")]
    DTODinoCollection GetDinoCollection();

它的实现代码:

public DTODinoCollection GetDinoCollection()
    {
        DinoCollection dinoCollection = dal.GetDinoCollection();
        return ConvertToDTODinoCollection(dinoCollection);

        //DTODinoCollection dinoCollection = new DTODinoCollection() { 
        //    new DTODino { DinoID = 1, Name = "test1", Health=10},
        //    new DTODino { DinoID = 2, Name = "test2", Health=11},
        //    new DTODino { DinoID = 3, Name = "test3", Health=12 } 
        //};
        //return dinoCollection;
    }

上面注释的代码是没有问题的值,但数据库数据只会从控制台调用,而不能从Web浏览器调用。

最后,这是我的服务的web.config:

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>

  <connectionStrings>
    <add name="DinoDatabase" connectionString="Data Source=MNLT084; Initial Catalog=DinoStats"  providerName="System.Data.SqlClient"/>
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

    <!--Endpoint added for help page functionality-->
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false"></standardEndpoint>
      </webHttpEndpoint>
    </standardEndpoints>
    <!--__________________________________________-->

  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
    To browse web app root directory during debugging, set the value below to true.
    Set to false before deployment to avoid disclosing web app folder information.
  -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

为了清楚起见,我已经尝试并且已经能够使用Angular js成功地在控制台和Web浏览器上检索测试数据,但是一旦我切换出来自数据库的数据的数据,它就会不再在网络浏览器上工作,只有控制台。即使我在VS中的控制台项目上看到正确地找到,加载和绑定到对象的数据。

我只是错过了一些小事吗?

-UPDATE -

我构建了一个WebAPI层并使用相同的EF层来调用SQL并检索数据库信息,它就像一个魅力。即使在前端显示使用角度也没问题。必须是我缺少的REST层中的一些设置或我错过的一些小细节。猜猜我在这个问题上坚持使用WebAPI。

1 个答案:

答案 0 :(得分:0)

你在&#34;中放置一个断点会发生什么? DinoCollection dinoCollection = dal.GetDinoCollection();&#34;它会返回数据吗? 您是否尝试在您调用automapper的地方设置断点?结果是完整的吗?还是空的?

您可以尝试的另一件事 - 启动&#39; Fiddler&#39; - 查看从服务器发送到您的控制台应用程序的实际流量以及角度。

我认为Chrome还能让您调查角度从webServer接收的流量(也许它无法解析结果?)

相关问题