EntityFramework with WCF - how to return EF entities

北城以北 提交于 2019-12-11 06:33:51

问题


I have a WCF service which works, but it uses Standard ADO.net to call some stored procedures.

This is a bit messy and annoying to maintain because any stored procs i call, i have to map to DataContracts i have to create manually and then return as JSON.

I would like to convert the WCF service to return EntityFramework entities (so i dont have to keep mapping and creating datacontracts manually).

I have something like this in my method, eg:

public List<GetStuff_Result> GetStuff(string param1)
{
            ...

            StuffEntities ctx = new DataContracts.StuffEntities();

            List<GetStuff_Result> list = ctx.GetStuff(5463, "test").ToList();

            return list;
}

I have setup the stored proc in the edmx model to return a GetStuff_Result (i imported the stored procedure and used the "create complex function")

I have debugged this, and i get 2 results in the list, but when my WCF method is called via the browser, i get a message on the browser (chrome):

No data received
Unable to load the web page because the server sent no data.
Here are some suggestions:
Reload this web page later.
Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data. 

Can anyone tell me how to fix this or at least why it's happening?

note (1): i have also right clicked on the edmx file and disabled lazy loading.

note (2): i have setup a DbContext generator

note (3): i use response format of WebMessageFormat.Json

thanks


回答1:


This problem is actually well suited for WCF Data Services (unrelated to WCF SOAP).

http://msdn.microsoft.com/en-us/library/dd744841.aspx

By default WCF Data Services will return data in XML format, however there is a request parameter that tells it to return JSON

http://blogs.msdn.com/b/writingdata_services/archive/2011/02/25/getting-json-out-of-wcf-data-services.aspx




回答2:


1) It always a good idea to test your application in the WCF test client before deployng it so you can see if the service call is valid

2) The reason why it happening is that it not really matter if you run direct store procedure or if you do it with EF the GetStuff_Result you still have to assign [DataMember] to every variable in your GetStuff_Result class so it will be serialized the EF stored procedure does not automatically serializes the result for you.




回答3:


Actually i found a better way of doing this

I can setup the edmx code generator to use this one: http://visualstudiogallery.msdn.microsoft.com/32c4660d-7e66-4c3a-b516-584f4f72b838

Its DbContext which has WCF support (so the objects it generates already have DataMember and DataContract properties)

This allows the objects to be returned in the service without the need to map EF objects to WCF objects like what COLD TOLD has suggested.



来源:https://stackoverflow.com/questions/14595544/entityframework-with-wcf-how-to-return-ef-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!