An existing connection was forcibly closed by the remote host

I got below error when I try to fetch information from database using WCF service. I was using model entities directly because of this my code not able to fetch data. So I used following technic and it worked well. Basically we need to use DTOs to do data serialization.

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

 

https://stackoverflow.com/questions/7161333/why-does-my-wcf-service-not-use-my-entity-model/7161377#7161377

Advertisements
Posted in: WCF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s