1

I got a problem with my WCF service. Here is the

    [OperationContract]
    [WebGet(UriTemplate = "/needs", ResponseFormat = WebMessageFormat.Json)]
    List<CustomerNeed> getAllCustomerNeeds();

When I go on the page which call this service, I got this error

GET http://localhost:666/rest/Service1.svc/needs net::ERR_CONNECTION_RESET

When I'm trying to return a string instead of a List, it works.

CustomerNeed is a class generate from my database via EntityFramework.

In my service, I'm only calling an other method which is in an other class;

    public List<CustomerNeed> getAllCustomerNeeds()
    {
        var needs = from cn in db.CustomerNeeds
                    select cn;

        List<CustomerNeed> list = new List<CustomerNeed>();

        foreach (CustomerNeed cusN in needs)
        {
            list.Add(cusN);
        }
        return list;
    }

Maybe is it because I have a foreign key in my table CustomerNeed ?

When I do "LINQ to entities" to import my database, do I have to import tables that were created because of many to many relation ?

2
  • can you show your implementation rather than your contract? Commented Mar 20, 2015 at 15:34
  • I add the implementation which is just a LINQ query Commented Mar 20, 2015 at 15:44

2 Answers 2

1

I will recommend you to create a simple custom class which will represent your CustomerNeeds database entity, initiate this object on the server side and pass to the client application. It can help you to avoid this problem and also it is recommended way to transfer data accross the WCF services.

In this case you need to do the next steps: 1) Create a public class CustomerNeeds and mark it with the DataContract attribute. For example:

[DataContract]
public class CustomerNeeds 
{
   [DataMember]
   public SomeDataType PropertyName {get; set;}
}

2) Initiate this object on the service, change return datatype in getAllCustomerNeeds() method from the entity class to the newly created class CustomerNeed and pass this data to the clien

And that`s all.

Sign up to request clarification or add additional context in comments.

5 Comments

I wonder if this could depend on the entity framework version being used. I use EF with WCF and have not had to decorate any of my autogenerated model classes with such attributes (and it works)
That's why I did't create DataContract classes. I saw on internet that with EF, it was not necessary. Anyway I'll try this way
Ok so I'm using DataContract now + Entities and it works. I just add a method that translate the entity from my database to my datacontract
@Weedoze just curious, what version of EntityFramework are you using? I don't need to do this for EF5
Oh this is interesting: stackoverflow.com/a/16881513/2312877 - it might actually have more to do with the .net framework version you're using.
0

You haven't shown where/what db is, but I'm assuming if you're using entity framework as your tag implies it's a entities context. You might be having some issues with the context already being disposed or not newed up correctly (though I would have expected you to receive a slightly different error if that's the case.)

It looks like you're going through some unnecessary steps in your function, I would think something like this would work:

public List<CustomerNeed> getAllCustomerNeeds()
{
    using (var db = new YourContext()) // plug in your context object
    {
        return db.CustomerNeeds.ToList();
    }
}

Additionally when you say it "works as a string" are you returning something small like "hello world"? you might need to take a look at your WCF configuration to make sure it can handle the amount of data you're trying to pass back and forth.

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.