我正在嘗試設置一個wcf rest服務,該服務僅返回恐龍實體的集合以便在網頁上顯示它們。該服務的問題是,我可以調用它的方法,並接收硬編碼的測試數據以及來自我的數據庫的數據以進行控制檯程序測試,但是,當我嘗試在瀏覽器中查看服務時,我只能接收硬編碼的測試數據,但不包括數據庫。當我從數據庫中獲取數據時,服務返回錯誤的請求錯誤。當我將數據硬編碼到服務中時,我得到了一個很好的json對象。WCF Rest服務僅將硬編碼數據返回給Web瀏覽器
在這裏與實體框架用於我的模型做數據從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>
只是要清楚,我曾嘗試,並已能夠在中控臺和採用了棱角分明的JS網頁瀏覽器成功獲取測試數據,但只要我將來自數據庫的數據的數據轉換出來,它就不會再通過網絡瀏覽器工作,只能在控制檯上工作。即使正確地找到,加載和綁定到對象的數據,只要我可以在VS中的控制檯項目上看到。
我只是想念一些小事情嗎?
-UPDATE-
我建了一個的WebAPI層和使用相同的EF層調用SQLand檢索數據庫中的信息,它就像一個魅力。即使在前端顯示使用角度沒有問題。必須在我缺少的REST層中進行一些設置或者我錯過了一些小細節。猜猜我在這個上堅持WebAPI。
我已經在該方法中放置了一個斷點,並可以看到信息已填充,使用automapper分配給新對象,並正確返回。這些信息與我所有的數據庫信息相符沒有空值,我可以看到。 – DapperMode
就fiddler而言,我可以將測試數據流看作一個很好的json對象集合。只要我從數據源切換到真實數據,它就會給我提供錯誤代碼400「錯誤請求」,但是從測試控制檯應用程序調用服務調用仍然有效,這是一個奇怪的部分。我已經使用了chrome開發工具,只要數據是測試數據,我就可以看到數據全部通過角度顯示在前端,但是一旦我切換,它就無法加載資源。 – DapperMode