Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的WCF服务不使用我的实体模型?_C#_Wcf_Entity Framework_Entity Model - Fatal编程技术网

C# 为什么我的WCF服务不使用我的实体模型?

C# 为什么我的WCF服务不使用我的实体模型?,c#,wcf,entity-framework,entity-model,C#,Wcf,Entity Framework,Entity Model,我在一起使用WCF服务和实体模型时遇到了一个问题。我已经从现有数据库创建了一个实体模型。这可以显示在下面 在任何来自“实体对象代码生成器”的控制台应用程序中使用我的类时没有任何问题 然后,我创建了WCF服务,接口如下: [ServiceContract] public interface IAuthorServices { [OperationContract] [WebGet( UriTemplate="GetNews")] List<Newspaper

我在一起使用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();

}
[服务合同]
公共接口IAuthorServices
{
[经营合同]
[WebGet(UriTemplate=“GetNews”)]
List GetNews();
[经营合同]
[WebGet(BodyStyle=WebMessageBodyStyle.Bare,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,UriTemplate=“GetAuthors”)]
列出GetAuthors();
[经营合同]
[WebGet(BodyStyle=WebMessageBodyStyle.Bare,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,UriTemplate=“GetAuthorTexts”)]
列出GetAuthorTexts();
[经营合同]
[WebGet(BodyStyle=WebMessageBodyStyle.Bare,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,UriTemplate=“GetTodaysTexts”)]
列出getToDataTexts();
[经营合同]
[WebGet(BodyStyle=WebMessageBodyStyle.Bare,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,UriTemplate=“GetExceptions”)]
列出GetExceptions();
}
然而,当我在服务类中实现这些方法并运行我的客户机应用程序时,我得到了一个错误,如

我怎样才能解决这个问题

问候,,
凯末尔

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

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

在这些情况下,您应该考虑使用DTOs(数据传输对象),这是<代码>报纸< /代码>类可以是:

的示例。
[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();
}
public List<NewspaperDTO> GetNews()
{
    using (var entities = new MyEntities())
    {
        return entities.Newspapers.Select(a => new NewspaperDTO(a)).ToList();
    }
}
public List GetNews()
{
return entities.papers.Select(a=>newnews-paperdto(a)).ToList();
}
另外,我注意到您的实体没有被处置(我的意思是在WCF服务中)。在服务的每种方法中,你都应该考虑使用这样的模式:

public List<NewspaperDTO> GetNews()
{
    return entities.Newspapers.Select(a => new NewspaperDTO(a)).ToList();
}
public List<NewspaperDTO> GetNews()
{
    using (var entities = new MyEntities())
    {
        return entities.Newspapers.Select(a => new NewspaperDTO(a)).ToList();
    }
}
public List GetNews()
{
使用(var entities=new MyEntities())
{
return entities.papers.Select(a=>newnews-paperdto(a)).ToList();
}
}

从我的第二个屏幕截图中可以看到,返回作者的GetAuthors方法工作正常,而GetPapers方法不工作。我所看到的是,如果我从“实体模型自跟踪对象生成器”创建实体对象,一切都会正常工作。然而,这一次,当我试图通过webHttpBinding使用REST服务发布我的服务时,它不会将我的对象序列化/反序列化为json对象,即使我填写了ResponseFormat。顺便说一句,如果我想序列化/反序列化为XML,它可以很好地工作,但实体对象应该由“自跟踪对象生成器”生成。对于这两个问题,我的答案是肯定的。顺便说一下,我有点难以理解到底发生了什么。即使你已经发布了类图,我也不确定在后台发生了什么。请尝试将您的问题的压缩解决方案链接到我,我将查看;)我不得不说,我只是跳到这里,因为我看不到图像(工作时被阻挡),我只是在猜测。您的实体是否相互引用?例如,您是否有一位家长,该家长是否有一系列引用该家长的孩子?WCF将尝试序列化这些内容,但由于无限递归,它将崩溃。我已经看过很多次了。有很多原因,因为这是行不通的,不管怎样,考虑一个不同的方法是合理的。我的意思是,客户是否需要了解诸如
EntityKey
EntityState
之类的属性?