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:

public class NewspaperDTO
    public NewspaperDTO(Newspaper newspaper)
        this.Name = newspaper.Name;
        this.Image = newspaper.Image;
        this.Link = newspaper.Link;
        this.Encoding = newspaper.Encoding;

    public string Name { get; set; }

    public string Image { get; set; }

    public string Link { get; set; }

    public string Encoding { get; set; }

And then in your service:

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

Posted in: WCF

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s