Click here to Skip to main content
15,917,702 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm developing a WCF Rest service with Json and Xml response. I have more than 100 stored procedure in my database.

My aim to avoid multiple operation contarct and get all the data from database so I have declared object as return type as below.

C#
[OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "GetDetails?type={type}")]
        object GetInfo();

        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "PostDetails?type={type}")]
        object PostInfo(object Entities);

        [OperationContract]
        [WebInvoke(Method = "PUT", UriTemplate = "EditDetails?type={type}")]
        object EditInfo(object Entities);

        [OperationContract]
        [WebInvoke(Method = "DELETE", UriTemplate = "DeleteDetails?type={type}")]
        object DeleteInfo(object Entities);



But i'm facing serialization problem because WCF should return response based on DataContract so operation contract should have respective DataContract

C#
[OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "GetUserDetails?type={type}")]
        List<UserModel> GetInfo();

[OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "GetXenDetails?type={type}")]
        List<XenModel> GetInfo();

[OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "PostGroupDetails?type={type}")]
        List<GroupModel> PostInfo(GroupEntities Entities);



Please any one suggest me which is better option:
1. Should I create more opertion contract with respective DataContract
2. Explicitly convert DB data to Json or Xml and return as object or String
3. Shall i return as Stream
Posted
Updated 12-Jan-16 3:49am
v7
Comments
Kornfeld Eliyahu Peter 10-Jan-16 9:52am    
WCF contracts are there to enforce typed conversation...
So if you are removing it - by returning JSON/XML string - why to use WCF at the first place?
mn.sathish 10-Jan-16 21:25pm    
Thanks for your valuable suggestion. Shall We create 100 Operation Contracts in WCF? If so, Whether it is wrong approach or correct approach?
Kornfeld Eliyahu Peter 11-Jan-16 1:47am    
There is no actual limit of how many contracts you may create...However it is hard to really advise you without knowing what do you want to achieve...
mn.sathish 11-Jan-16 2:19am    
From MVC I am calling WCF Rest service to get DB data which contains more than 150 stored procedure. My aim to create common operation contract to call appropriate SP and bring back result set to MVC.
Tell me how to achieve
Kornfeld Eliyahu Peter 11-Jan-16 2:54am    
I think that there should not be a problem to create a single contract for that...The question is what those SPs are returning? A table? A single value? If it is a table, should you separate different tables as different types, or every table is a table...
I would also ask myself, what I'm doing with the result from those SPs? Some processing? Going to display? Pass on?

What I did in a similar Situation was using a XMLDocument as return/Parameter type which I serialized and deserialized - was a stupid solution. Others mentioned it - when I do this why use WCF in the first place?
If you don't have an object-model (classes) corresponding to your datamodel (tables) why not generate one? - T4 or use an OR-Mapper (just for generating Model-classes), put some attributes on the properties to make it a datacontract and have separate implementations (Service-Methtods) for each tables CRUD-Operation (they will then execute the operations through your SPs, and on the implementation side (not the contract) you can come up with some generic methods and not duplicate the code accessing data xxx times).
Just my 2 c...
 
Share this answer
 
I switch over to ASP .Net Web API.
WCF does not support anonymous return type whereas WEB API support anonymous return type.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900