【问题标题】:Why does my WCF Service not use my Entity Model?为什么我的 WCF 服务不使用我的实体模型?
【发布时间】:2011-08-23 12:55:38
【问题描述】:

我在同时使用 WCF 服务和实体模型时遇到了问题。我已经从我现有的数据库中创建了一个实体模型。如下图所示;

在来自“实体对象代码生成器”的任何控制台应用程序中使用我的类时没有任何问题。

然后,我用下面的接口创建了 WCF 服务:

    [ServiceContract]
public interface IAuthorServices
{
    [OperationContract]
    [WebGet( UriTemplate="GetNews")]
    List<Newspaper> GetNews();

    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetAuthors")]
    List<Author> GetAuthors();

    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetAuthorTexts")]
    List<AuthorText> GetAuthorTexts();

    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetTodaysTexts")]
    List<AuthorText> GetTodaysTexts();

    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetExceptions")]
    List<KoseYazilari.Exception> GetExceptions();

}

但是,当我在服务类中实现这些方法并运行我的客户端应用程序时,我收到了类似的错误

我怎样才能摆脱这个问题?

问候, 凯马尔

【问题讨论】:

    标签: c# wcf entity-framework entity-model


    【解决方案1】:

    您的实体是否标有DataContract 属性?你确定它们是可序列化的吗?

    编辑:通过查看您的代码,您似乎正在直接使用您的实体。这不是一个好的做法,因为(即使您的代码正在运行)我认为您不需要额外的属性,例如 Entity Framework 自动生成的属性。

    在这种情况下,您应该考虑使用 DTO(数据传输对象),这是 Newspaper 类的示例:

    [DataContract]
    public class NewspaperDTO
    {
        public NewspaperDTO(Newspaper newspaper)
        {
            this.Name = newspaper.Name;
            this.Image = newspaper.Image;
            this.Link = newspaper.Link;
            this.Encoding = newspaper.Encoding;
        }
    
        [DataMember]
        public string Name { get; set; }
    
        [DataMember]
        public string Image { get; set; }
    
        [DataMember]
        public string Link { get; set; }
    
        [DataMember]
        public string Encoding { get; set; }
    }
    

    然后为您服务:

    public List<NewspaperDTO> GetNews()
    {
        return entities.Newspapers.Select(a => new NewspaperDTO(a)).ToList();
    }
    

    P。 S. 我注意到您的实体没有被处置(我的意思是在 WCF 服务内部)。您应该考虑在服务的每个方法中使用这样的模式:

    public List<NewspaperDTO> GetNews()
    {
        using (var entities = new MyEntities())
        {
            return entities.Newspapers.Select(a => new NewspaperDTO(a)).ToList();
        }
    }
    

    【讨论】:

    • 从我的第二个屏幕截图中可以看出,返回作者的 GetAuthors 方法工作正常,而 GetNewspapers 方法不工作。我所看到的是,如果我从“实体模型自我跟踪对象生成器”创建我的实体对象,一切正常。但是,这一次,当我尝试通过 webHttpBinding 使用 REST 服务发布我的服务时,即使我填充了 ResponseFormat,它也不会将我的对象序列化/反序列化为 json 对象。顺便说一句,如果我想序列化/反序列化为 XML,它工作正常,但实体对象应该由“自我跟踪对象生成”生成。
    • 对你的两个问题,顺便说一句,我的回答是肯定的。
    • 我有点难以理解发生了什么。即使您发布了类图,我也不确定后台发生了什么。尝试将您的问题的压缩解决方案链接给我,我会看看;)
    • 我不得不说我只是跳到这里,因为我看不到图像(在工作中被阻止),我只是在猜测。您的实体是否相互引用?例如,您是否有一个具有引用该父级的子级列表的父级? WCF 将尝试对这些进行序列化,并且由于无限递归,它会爆炸。我已经看过几次了。
    • 有很多原因,因为这行不通,无论如何考虑不同的方法是合理的。我的意思是,客户是否需要了解EntityKeyEntityState 之类的属性?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多