在开发.NET一个新的RESTful Web服务 - 我应该在哪里开始呢? ASP.NET-MVC,WCF? [英] Developing a new RESTful web service in .NET -- where should I begin? ASP.NET-MVC, WCF?

查看:123
本文介绍了在开发.NET一个新的RESTful Web服务 - 我应该在哪里开始呢? ASP.NET-MVC,WCF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的目标是建立一个然后,我将通过jQuery消费和服务标准的基于Web的前端,移动设备的胖客户端,并极有可能是WPF桌面应用程序。

The objective is to build a service that I will then consume via jQuery and a standards based web front-end, mobile device "fat-clients," and very likely a WPF desktop application.

这似乎是WCF将是一个不错的选择,但我从来没有建立一个RESTful服务与WCF,所以我不知道在哪里甚至开始对这一做法。

It seems like WCF would be a good option, but I've never built a RESTful service with WCF, so I'm not sure where to even begin on that approach.

另一种选择,我想的是使用ASP.NET MVC中,增加了一些自定义的航线,增加一些控制措施,并使用不同的看法,推动了JSON,XML和其它返回类型。

The other option I'm thinking about is using ASP.NET MVC, adding some custom routes, add a few controller actions and using different views to push out JSON, xml, and other return types.

这个项目主要是一个学习锻炼自己,我希望花一些额外的时间和做正确的,所以我有一个更好的undertanding的部分是如何结合在一起的。

This project is mostly a learning exercise for myself, and I'd like to spend some extra time and do it "right" so I have a better undertanding of how the pieces fit together.

所以,我的问题是,哪种方式我应该用它来建立这个RESTful服务,什么都做这样的一些优势?

So my question is this, which approach should I use to build this RESTful service, and what are some advantages of doing it that way?

推荐答案

通常情况下,我会说WCF任何一种托管的贡献莫过于,但在使用JSON作为一种序列化机制RESTful服务的具体情况,我preFER ASP.NET MVC(我将其称为ASP.NET的这个答案的其余部分)。

Normally, I would say WCF for any kind of hosted serice, but in the specific case for RESTful services using JSON as a serialization mechanism, I prefer ASP.NET MVC (which I will refer to as ASP.NET for the remainder of this answer).

其中的第一个原因是因为路由机制。在WCF中,你必须对合同,这是一切都很好定义它,但如果你要快速更改你的路由,从我的角度来看,它更容易使用ASP的路由机制做这些。 NET。

One of the first reasons is because of the routing mechanism. In WCF, you have to define it on the contract, which is all well and good, but if you have to make quick changes to your routing, from my point of view, it's much easier to do them using the routing mechanism in ASP.NET.

另外,上面的一点,如果你有暴露在WCF中的多个接口的多个服务,就很难让你的URL结构的完整映像(这很重要),而在ASP.NET你(通常)都的路线的分配在一个地方。

Also, to the point above, if you have multiple services exposed over multiple interfaces in WCF, it's hard to get a complete image of your URL structure (which is important), whereas in ASP.NET you (typically) have all of the route assignments in one place.

有关ASP.NET的第二件事情是,你将有机会获得所有的ASP.NET是已知的(请求,响应,服务器,等等,等等),露出一个HTTP时,这是必不可少的内在对象特异性终点(这是你所创造的东西)。当然,你可以使用很多的WCF这些同样的事情,但你必须明确告诉WCF,你是这样做的,然后使用设计你的服务的初衷。

The second thing about ASP.NET is that you are going to have access to all of the intrinsic objects that ASP.NET is known for (Request, Response, Server, etc, etc), which is essential when exposing an HTTP-specific endpoint (which is what you are creating). Granted, you can use many of these same things in WCF, but you have to specifically tell WCF that you are doing so, and then design your services with that in mind.

最后,通过亲身体验,我发现,在<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx"相对=nofollow> DataContractJsonSerializer 不处理的DateTimeOffset 值太好了,这是用它可以通过人在多个时区被称为服务(通过任何端点)工作时,你应该使用了的DateTime 类型。在ASP.NET中,有不同的序列化,你可以使用,如果你愿意,你可以创建自己的的ActionResult ,它使用一个自定义序列为您服务。我个人preFER的 JSON.Net串行

Finally, through personal experience, I've found that the DataContractJsonSerializer doesn't handle DateTimeOffset values too well, and it is the type that you should use over DateTime when working with a service (over any endpoint) which can be called by people over multiple timezones. In ASP.NET, there is a different serializer that you can use, or if you want, you can create your own ActionResult which uses a custom serializer for you. I personally prefer the JSON.Net serializer.

一个关于JSON.Net序列化和ASP.NET的好东西,我喜欢的是你可以使用匿名类型吧,如果你是聪明的。如果你在一个非泛型类型,然后委托给一个内部泛型类型,可以使用类型推断轻松地利用匿名类型的序列化的返回值(假设他们都是一次性的,当然,如果您创建一个静态泛型方法有始终返回的结构,你应该把它定义和使用)。

One of the nice things about the JSON.Net serializer and ASP.NET that I like is that you can use anonymous types with it, if you are smart. If you create a static generic method on a non-generic type which then delegates to an internal generic type, you can use type inference to easily utilize anonymous types for your serialized return values (assuming they are one-offs, of course, if you have a structure that is returned consistently, you should define it and use that).

还应该提到的是,你不必完全折扣WCF,如果开发一个RESTful服务。如果你正在推动一个原子或RSS的服务送出那么类中的 System.ServiceModel.Syndication 的命名空间的大规模这些饲料的建设和系列化的帮助。创建的ActionResult 类的简单的子类采取的 SyndicationFeed ,然后将其序列化到输出流时,的ActionResult 执行是很简单的。

It should also be mentioned that you don't have to completely discount WCF if developing a RESTful service. If you are pushing an ATOM or RSS feed out from your service then the classes in the System.ServiceModel.Syndication namespace of massive help in the construction and serialization of those feeds. Creating a simple subclass of the ActionResult class to take an instance of SyndicationFeed and then serialize it to the output stream when the ActionResult is executed is quite simple.

这篇关于在开发.NET一个新的RESTful Web服务 - 我应该在哪里开始呢? ASP.NET-MVC,WCF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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