3

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;

enter image description here

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

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

enter image description here

How can I get rid of this problem?

Regards, KEMAL

David Clarke
  • 12,888
  • 9
  • 86
  • 116
kkocabiyik
  • 4,246
  • 7
  • 30
  • 40

1 Answers1

4

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

EDIT: 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.

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. 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();
    }
}
as-cii
  • 12,819
  • 4
  • 41
  • 43
  • As you can see from my second Screenshot, GetAuthors method which returns the Authors is working fine whereas GetNewspapers method is not working. What I have seen is that if I create my Entity Object from "Entity Model Self-Tracking Object Generator", everything works fine. However, this time, when I try to publish my service with REST services over webHttpBinding, It doesn't serialize/deserialize my object as json object even though I fill ResponseFormat. Btw, It works fine if I want to serialize/deserialize as XML but again entity object should be generated by "Self-Tracking Object Gen." – kkocabiyik Aug 23 '11 at 13:21
  • To your both question, my answer is yes by the way. – kkocabiyik Aug 23 '11 at 13:22
  • It is a bit difficult for me to understand what is going on. Even if you have posted the class diagram I am not sure of what is happening on the background. Try to link me a zipped solution of your problem and I will take a look ;) – as-cii Aug 23 '11 at 15:26
  • 1
    I have to say I'm just jumping in here, as I can't see the images (blocked at work), I'm just throwing out a guess. Do your entities reference each other? For example, do you have a parent with a list of children that refernce the parent? WCF will attempt to serialize these and it'll bomb out because of infinite recursion. I have seen this a couple of times. – Mike M. Aug 24 '11 at 20:39
  • 1
    There are a lot of reasons because this could not work, anyway it is reasonable to consider a different approach. I mean, does the client need to know about properties like `EntityKey` or `EntityState`? – as-cii Aug 25 '11 at 11:33