WCF RESTful服务和EF问题

时间:2012-03-25 14:29:34

标签: .net wcf entity-framework rest

我首先使用EF代码在Azure上创建数据库,现在我想使用WCF REST公开数据。

但是,我有一些奇怪的行为。当我想返回一个(即项目)列表时,我的浏览器无法简单地检索它。

当我重命名方法以便返回字符串并返回string.item时,它可以工作:/

我的 Web.config

<configuration>
 <connectionStrings>
  <add name="CijenolomciContext" connectionString="Server=tcp:*.database.windows.net;Database=myDataBase;User ID=*;Password=*;Trusted_Connection=False;Encrypt=True;" providerName="System.Data.EntityClient"/>
</connectionStrings>
<system.serviceModel>
  <services>
    <service name="Cijenolomci2.Servis.Service" behaviorConfiguration="ServiceBehavior">
      <endpoint address="" binding="webHttpBinding" contract="Cijenolomci2.Servis.IService" behaviorConfiguration="web"/>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="ServiceBehavior">
        <serviceMetadata httpGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="false"/>
      </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
      <behavior name="web">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>
<system.web>
  <compilation debug="true"/>
</system.web>
</configuration>

项目类:

[DataContract]
public class Item
{
    [DataMember]
    public int ItemID  { get; set; }

    [Required(AllowEmptyStrings=false, ErrorMessage="You must enter a name!")]
    [MaxLength(50, ErrorMessage = "Name must be 50 characters or less")]
    [DataMember]
    public string Name { get; set; }

    [MaxLength(100, ErrorMessage = "Description must be 50 characters or less")]
    [DataMember]
    public string Description { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter a OldPrice!")]
    [DataMember]
    public Decimal OldPrice { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter a NewPrice!")]
    [DataMember]
    public Decimal NewPrice { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter a SaleStarts date!")]
    [DataMember]
    public DateTime SaleStarts { get; set; }

    [DataMember]
    public DateTime? SaleEnds { get; set; }

    [DataMember]
    public int CategoryID { get; set; }

    [DataMember]
    public virtual Category Category { get; set; }
}

IService:

[OperationContract]
    [WebGet(UriTemplate = "/items/", ResponseFormat = WebMessageFormat.Json)]
    List<Item> GetItems();

Service.svc:

public List<Item> GetItems()
    {
        var result = db.Items.AsNoTracking().ToList();           

        return result;
    }

1 个答案:

答案 0 :(得分:1)

我可以提出的几个建议是:

您的Item个类属性之一是:public virtual Category Category { get; set; } Category类也是[DataContract]吗?

检查Windows事件查看器并查看是否记录了错误。有时,如果WCF错误足够严重,并且它是IIS托管的,那么w3wp.exe进程会将错误丢弃到Windows日志中。

启用WCF跟踪日志记录(仅限Google“wcf跟踪日志记录”),并查看它是否在服务器上记录错误。