为什么我的WCF服务不能使用我的实体模型? [英] Why does my WCF Service not use my Entity Model?

查看:223
本文介绍了为什么我的WCF服务不能使用我的实体模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WCF服务和实体模型一起有一个问题。我从现有的数据库创建的实体模型。这可以在下面所示;

I got a problem on using WCF service and Entity Model with together. I have created an Entity Model from my existing database. This can be shown below;

而使用任何控制台的一个应用从实体对象代码生成器来我的班没有任何问题。

There isn't any problem while using my classes in any console applicaton coming from "Entity Object Code Generator".

然后,我创建WCF服务与界面如下图:

Then, I created WCF Service with Interface Below:

    [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();

}



然而,当我实现了服务类和运行这些方法我的客户端应用程序,我得到了像

However when I implement these methods in a service class and run my client application, I got an error like

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

How can I get rid of this problem?

问候,
KEMAL

Regards, KEMAL

推荐答案

是你的实体打上 DataContract 属性? ?你要确保他们是序列化

Are your entities marked with a DataContract attribute? Are you making sure that they are serializable?

编辑:通过查看你的代码似乎你直接用你的实体。这是不是一个好的做法,因为(即使你的代码工作),我不认为你需要额外的属性一样,实体框架自动生成的。

By looking at your code it seems that you are using your entities directly. This is not a good practice because (even if your code was working) I don't think you want extra properties like the ones that Entity Framework auto-generates.

在这些情况下,你应该考虑使用的DTO(数据传输对象),这是报纸类怎么可能一个例子:

In these case you should consider to use DTOs (Data Transfer Objects), this is an example of how Newspaper class could be:

[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; }
}



然后在你的服务:

And then in your service:

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



P上。 S.我注意到,您的单位没有设置(里面的WCF服务我的意思)。你应该考虑在你的服务的每个方法使用这样的模式:

P. S. I have noticed that your entities are not disposed (inside the WCF service I mean). You should consider using a pattern like this in every method of your service:

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

这篇关于为什么我的WCF服务不能使用我的实体模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆