What framework to use for RESTful Services in .net

2019-03-08 05:34发布

问题:

I know that similar questions have been asked, but most of them are out of date. So here we go again :). I need to implement a complete REST service layer for our application. The problem that i have is which framework would be the best to solve this problem. I just need a nice framework that lets me focus on the problem and not on the REST or whatever is required. Authentication is a required feature. Here are some of my ideas; what do you think?

  • WCF: In my opinion this is a overloaded framework that makes things complicated.
  • ServiceStack: Seems to be a nice, lightweight, open source alternative. But what if they decided to stop development?
  • Custom implementation using asp.net mvc such as this, but why reinvent the wheel?

回答1:

I originally started ServiceStack because of the inefficiency (development and runtime) and friction imposed in creating web services with alternative .NET frameworks.

3-4x Faster Json Serialization than MVC

ServiceStack has a strong focus performance as we believe it provides the best end-user UX which is why it comes in-built with a strong set of Caching providers including the fastest JSON Serializer for .NET - 3-4x times faster than the serializers shipped with .NET and MVC (its default JavaScriptSerializer is the slowest in .NET). For max performance there's no runtime reflection or Regular Expressions used. It employs smart non-linear Route matching and you're recommended to use the much faster built-in Caching providers to work around the poor performance of ASP.NET's Session.

Focused on typed, iterative, code-first development

ServiceStack lets you develop strong-typed web services promoting best practices out-of-the-box using the minimum amount of code and automatically without any code-gen, config, pre/post build-steps, etc.

Example of a simple Hello World service:

public class Hello { public string Name { get; set; } }
public class HelloResponse { public string Result { get; set; } }

public class HelloService : IService 
{
    public object Get(Hello request) 
    {
        return new HelloResponse { Result = "Hello, " + request.Name };
    }
}

With just these classes, all your web services are automatically made available in a variety of different formats (JSON, XML, JSV, CSV, SOAP) all out-of-the-box with zero effort.

Example of Strong Typed Client API using C#:

var client = new JsonServiceClient("http://localhost/Service");
var response = client.Send<HelloResponse>(new Hello { Name = "World!" });

JavaScript example using jQuery:

$.getJSON("http://localhost/Service/hello/World!", function(r) {
    console.log(r.Result);
});

Development friendly

Because visualizing web services is important when iteratively developing web services, the default Content-Type when viewing web services in a browser is a human friendly JSON HTML5 Report format (also available stand-alone at http://ajaxstack.com/jsonreport/) which enables you to visualize the response of your web services in a glance.

You also get an automatically generated metadata page (that you can annotate with your own custom description) which serves as a great way to document your web service API.

But what if they decided to stop development

As the creator of ServiceStack I don't see myself abandoning development in the foreseeable future. I build systems with it daily simply because I find it's a cleaner, faster, more productive framework to develop with.

Promotes best-practices

There are very few .NET web services frameworks that promote a DTO-first message-based architecture enabling the Service Interface pattern - A web services best-practice commonly seen in the Java ecosystem making it easy to develop batch-full coarse grain SOA-based web services.

There is 0 risk it will be abandoned in favour of another .NET web service framework. Simply because we don't believe any other .NET framework actively promotes web services best-practices (i.e. DTO / Remote Façade and Service Interface patterns) and a primary focus on performance.

But even so as an Open Source project with nearly 20 contributors, this fear is mitigated. How many proprietary, closed-source frameworks have MS abandoned and forced everyone to move onto a successor? Open source software evolves, it doesn't get abandoned and rewritten.

The entire source code for ServiceStack lives under http://github.com/ServiceStack there is no lock-in and GitHub makes it easy for anyone to fork and continue development as many have already done.

Works everywhere

Finally, ServiceStack can run on any ASP.NET host in IIS 6/7 on Windows or Linux/OSX using Mono. It also supports a stand-alone HttpListener host allowing you to run it without a web server, i.e. embedded in any Console or Windows application, inside a Windows Service and has even hosted inside a MonoTouch iPhone application.



回答2:

Been playing with Nancy myself lately and I'm also considering Manos de Mono. Here's the an example from the home page on Nancy.

public class HelloModule : NancyModule
{
    public HelloModule()
    {
        Get["/"] = parameters => "Hello World";
    }
}


回答3:

For me, the easiest and cleanest solution would be to implement the services as controllers in ASP.NET MVC3 with methods that return a JsonResult.

Advantages:

  • The MVC framework does the heavy lifting for you

  • You can implement the model validation using attributes instead of code

  • XCopy deployment to any version of IIS



回答4:

If I was starting this today I would choose from your third option of doing something Custom in ASP.NET MVC3 or using one of the frameworks below.

  • WCF Preview 4
    • Its been re-written by Glenn Block
    • Here's a sample REST project RestBucks
  • OpenRasta
    • Demo application here


回答5:

Thje Shoulders Of Giants solution is now available via CodePlex and NuGet... renamed as Resources Over MVC.