【问题标题】:WCF Rest Service only returns hard-coded data to web browserWCF Rest Service 仅将硬编码数据返回到 Web 浏览器
【发布时间】:2016-01-21 19:35:33
【问题描述】:

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

这是我使用实体框架从 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.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 的控制台项目中看到的而言,即使数据被正确地找到、加载并绑定到对象。

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

-更新-

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

【问题讨论】:

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


    【解决方案1】:

    在“DinoCollection dinoCollection = dal.GetDinoCollection();”中设置断点会发生什么它返回数据吗? 您是否尝试在调用 automapper 的地方设置断点?结果满了吗?还是空的?

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

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

    【讨论】:

    • 我在该方法处设置了一个断点,可以看到信息已填充,使用自动映射器分配给新对象,并正确返回。该信息与我的所有数据库信息相匹配。我看不到任何空值。
    • 就提琴手而言,我可以看到测试数据流过就像一个不错的 json 对象集合一样。一旦我从数据源切换到真实数据,它只会给我错误代码 400“错误请求”,但是当从测试控制台应用程序调用时,服务调用仍然有效,这是奇怪的部分。我使用了chrome开发工具,只要是测试数据,我就可以看到数据一直通过角度并显示在前端,但是一旦我再次切换,它就无法加载资源。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 2016-02-03
    • 1970-01-01
    • 2013-10-01
    相关资源
    最近更新 更多