在.NET中開發新的RESTful Web服務 - 我應該從哪里開始? ASP.NET-MVC,WCF?

[英]Developing a new RESTful web service in .NET — where should I begin? ASP.NET-MVC, WCF?


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.

目標是構建一個服務,然后我將通過jQuery和基於標准的Web前端,移動設備“胖客戶端”以及很可能是WPF桌面應用程序來使用。

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.

看起來WCF會是一個不錯的選擇,但我從來沒有用WCF構建RESTful服務,所以我不確定從哪里開始這種方法。

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.

我正在考慮的另一個選項是使用ASP.NET MVC,添加一些自定義路由,添加一些控制器操作並使用不同的視圖來推出JSON,xml和其他返回類型。

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.

這個項目主要是我自己的學習練習,我想花一些額外的時間來做“正確”,這樣我就可以更好地了解各個部分是如何組合在一起的。

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?

所以我的問題是,我應該使用哪種方法來構建這種RESTful服務,這樣做有什么好處呢?

3 个解决方案

#1


8  

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適用於任何類型的托管服務,但在使用JSON作為序列化機制的RESTful服務的特定情況下,我更喜歡ASP.NET MVC(我將在本答案的其余部分中將其稱為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中,你必須在合同上定義它,這一切都很好,但如果你必須快速更改路由,從我的角度來看,使用ASP中的路由機制更容易。凈。

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.

此外,對於上述問題,如果在WCF中通過多個接口公開多個服務,則很難獲得URL結構的完整圖像(這很重要),而在ASP.NET中(通常)具有所有路徑在一個地方完成任務。

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.

關於ASP.NET的第二件事是你將能夠訪問ASP.NET所知的所有內部對象(請求,響應,服務器等等),這在暴露特定於HTTP的端點時是必不可少的。 (這是你正在創造的)。當然,您可以在WCF中使用許多相同的東西,但是您必須明確告訴WCF您正在這樣做,然后考慮到這一點設計您的服務。

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.

最后,通過個人經驗,我發現DataContractJsonSerializer不能很好地處理DateTimeOffset值,並且它是在使用服務(通過任何端點)時可以在DateTime上使用的類型,可以由人們調用多個時區。在ASP.NET中,您可以使用不同的序列化程序,或者如果需要,您可以創建自己的ActionResult,它為您使用自定義序列化程序。我個人更喜歡JSON.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).

我喜歡的JSON.Net序列化程序和ASP.NET的一個好處是你可以使用匿名類型,如果你很聰明的話。如果在非泛型類型上創建靜態泛型方法然后委托給內部泛型類型,則可以使用類型推斷來輕松利用序列化返回值的匿名類型(假設它們是一次性的,當然,如果你有一個一致返回的結構,你應該定義它並使用它)。

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.

還應該提到的是,如果開發RESTful服務,則不必完全折扣WCF。如果要從服務中推送ATOM或RSS源,那么System.ServiceModel.Syndication命名空間中的類可以幫助構建和序列化這些源。創建ActionResult類的簡單子類以獲取SyndicationFeed的實例,然后在執行ActionResult時將其序列化為輸出流非常簡單。

#2


4  

Here is a a thought that may help you make the decision between ASP.NET MVC and WCF. In the scenarios you describe, do you expect to need to use a protocol other than HTTP?

這是一個可以幫助您在ASP.NET MVC和WCF之間做出決定的思想。在您描述的場景中,您是否希望使用HTTP以外的協議?

WCF is designed to be transport protocol agnostic and so it is very different than ASP.NET. It has channels and bindings, messages, service contracts, data contracts and behaviours. It provides very little in the way of guidance when it comes to building distributed applications. What it gives you is a clean slate to build on.

WCF旨在與傳輸協議無關,因此它與ASP.NET非常不同。它具有渠道和綁定,消息,服務合同,數據合同和行為。在構建分布式應用程序時,它幾乎沒有提供指導。它給你的是一個干凈的石板建立。

ASP.Net MVC is naturally a Http based framework. It deals with HTTP verbs, media types, URLs, response headers and request headers.

ASP.Net MVC自然是一個基於Http的框架。它處理HTTP動詞,媒體類型,URL,響應頭和請求頭。

The question is which model is closer to what you are trying to build?

問題是哪種模型更接近您要構建的模型?

Now you mentioned ReST. If you really do want to build your distributed applications following the ReST constraints then you would be better to start with OpenRasta. It will guide you down that path.

現在你提到了ReST。如果您真的想按照ReST約束構建分布式應用程序,那么最好從OpenRasta開始。它將引導你走這條路。

You can do Rest in ASP.Net MVC and you can do it in WCF, but with those solutions, you will not fall into the pit of success ;-)

你可以在ASP.Net MVC中做Rest,你可以在WCF中完成它,但是使用這些解決方案,你不會陷入成功的陷阱;-)

#3


1  

Personally, I am not crazy about implementing REST services in WCF. I find the asp.net mvc framework a more natural programming model for this.

就個人而言,我並不是在WCF中實現REST服務。我發現asp.net mvc框架是一個更自然的編程模型。

The implementor of http://atomsite.net/ originally implemented the atompub specification in WCF and then rewrote the entire service using asp.net mvc. His experience echoed my comment above that for a pure REST service asp.net mvc is the way to go.

http://atomsite.net/的實現者最初在WCF中實現了atompub規范,然后使用asp.net mvc重寫了整個服務。他的經驗與我上面的評論相呼應,對於純粹的REST服務,asp.net mvc是要走的路。

The only exception would be if I wanted to potentially expose a service in a restful and non restful way. Or if I was exposing an existing WCF service via REST.

唯一的例外是,如果我想以一種寧靜和非寧靜的方式展示服務。或者,如果我通過REST公開現有的WCF服務。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/02/15/725dc0f8be4e927b0208bcf2c93c4951.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com