在WCF Rest Service中可以有超过100个操作合同 [英] Can have more than 100 Operation Contract in WCF Rest Service

查看:69
本文介绍了在WCF Rest Service中可以有超过100个操作合同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个带有Json和Xml响应的WCF Rest服务。我的数据库中有超过100个存储过程。



我的目标是避免多个操作contarct并从数据库中获取所有数据,因此我将对象声明为返回类型为下面。



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.

[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);







但是我面临序列化问题,因为WCF应该根据DataContract返回响应所以操作合同应该有各自的DataContract






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

[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);







请任何人建议我哪个是更好的选择:

1.我是否应该与相应的DataContract创建更多的opertion合约

2.将DB数据显式转换为Json或Xml并作为对象或字符串返回

3。我应该返回Stream




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

推荐答案

我在类似情况下所做的是使用XMLDocument作为返回/参数类型,我将其序列化和反序列化 - 这是一个愚蠢的解决方案。其他人提到它 - 当我这样做时为什么首先使用WCF?

如果你没有对应你的datamodel(表)的对象模型(类)为什么不生成一个? - T4或使用OR-Mapper(仅用于生成Model-classes),在属性上放置一些属性以使其成为datacontract,并为每个表CRUD-Operation分别实现(Service-Methtods)(然后它们将执行操作通过你的SP,在实现方面(而不是合同),你可以提出一些通用的方法,而不是重复访问数据xxx次的代码)。

只是我的2 c ...
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...


我切换到ASP .Net Web API。

WCF不支持匿名返回类型,而WEB API支持匿名返回类型。
I switch over to ASP .Net Web API.
WCF does not support anonymous return type whereas WEB API support anonymous return type.


这篇关于在WCF Rest Service中可以有超过100个操作合同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆