【发布时间】: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