I just built a asp.net 2.0 web site. Now I need add REST web service so I can communicate with another web application. I've worked with 2 SOAP web service project before, but have no experise with REST at all. I guess only a coupleweeks would works fine. after googling, I found it's not that easy.
This is what I found:
There is NO REST out of box of asp.net.
WCF REST Starter Kit Codeplex Preview 2 base on .net 3.5 and still in beta
Rest ASP.NET Example
REST Web Services in ASP.NET 2.0 (C#)
Exyus
Handling POST and PUT methods with Lullaby
ADO.NET Data Service
...
Now my question,
a) Is a REST solution for .net 2.0? if yes, which one is best solution?
b) if I have to, how hard to migrate my asp.net from 2.0 to 3.5? is it as simple as just compile, or I have to change a lot code?
c) WCF REST Starter Kit is good enough to use in production?
d) Do I have to learn WCF first, then WCF REST Starter Kit? where is the best place to start?
I appreciate any help here.
Thanks
Wes
If your looking for a project that templates a REST service, you're correct in saying there is no out of the box solution. However, RESTful web services are possible using WCF. The key part is to use several attributes when defining your service functions that let the .NET framework know that the function is not expecting SOAP. The main attribute to use is the WebInvoke attribute.
Here is an example from developer.com:
[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "/admin/post/{id}")]
void UpdatePost(string id, Post post);
The above code will actually be defined in an interface for your web service. The interface is created automatically when you create your WCF web service project. The actual code for the function will be placed in the class used to implement the web service.
Check out the article on developer.com for a full tutorial. It might seem overwhelming at first if your new to WCF, but after you dive into it, I'm sure you'll start to pick things up quickly. Here is the link for the artile: http://www.developer.com/net/article.php/10916_3695436_1
To answer all of your questions,
a) In .NET 2.0 you should be able to build RESTful services using WSE2.0, but if you have the option to use .NET 3.5, I would strongly recommend going the route of WCF since it is much easier and is designed with REST in mind.
b) Converting your project won't be hard at all. It's just a matter of targetting the new version of the framework in your project settings. Converting a web service from a WSE2.0 service to a WCF service will be a bit trickier though. The easiest way to do so would be to copy the code from each of the different web service functions into the class where you implement the new version of the function. Copy-Paste shinanigans :)
c) I'm not sure what this starter kit is that you're referring to. RESTful web services should be fully supported in WCF which was fully released as of 3.5
d) It would be helpful to understand WCF at least a little before beginning, but it's not crutial to understand it completely in order to get started. I would recommend just reading through the MSDN article on WCF at least once, and then begin working. I'm sure you will come across other questions as you begin, but you can look up those parts as you come across them.
Anyway, I hope this information helps. Good luck to you.
Edit
Some improvements have been made in the REST world. As Darrel Miller mentioned in the comments, WCF was not in fact built with REST in mind. I mis-spoke previously. In fact the framework is built with SOAP in mind and the WebInvoke attribute fills the gap. Although there is a lot of debate around the topic (Web API vs WCF REST), ASP.NET Web API is a new option for building REST services in .NET. I would strongly recommend that anyone who reads this post and is able to use .NET 4.5 in their project look into it as an option.
If you want a framework built with REST in mind, you should have a look at OpenRasta...
http://openrasta.org/
You could look at using ASP.NET MVC as a RESTful web services platform. WCF is probably the way to go in the long run, but MVC should easily handle it. Your actions would just need to be set up to return JSON or XML, depending on how you want to serialize it. MVC offers both a JsonResult and fully customizable ContentResult -- i.e., you serialize your response to a string property on the result and set it's type and encoding.
NOTE: MVC does require 3.5 SP1 so it's not going to be a 2.0 solution. If you require 2.0, you'll need to look elsewhere.
Use WCF REST.
You can use it today. The WCF REST library is ready to go, usable in production.
The WCF Starter Kit (see http://msdn.microsoft.com/en-us/netframework/cc950529.aspx) is something else. It delivers extra stuff; it includes a set of VS project templates, docs, samples and a few tools for building REST apps (client or server) with WCF. One cool tool is the "Paste Xml as Type" command add-in to Visual Studio.
On the other hand, building a REST client, in the general case, I'd recommend taking advantage of the HttpClient assembly in the starter kit (Microsoft.Http.dll). It's small, simple, and useful. A low-risk and high-value dependency even at the current "preview" status of the Starter Kit.
If you were consuming just a single REST service, then you don't need the general Microsoft.Http.dll assembly; you can do all the stuff it does in some code that uses WebRequest. But if you wanted a more general flexible class for manipulating client-side REST requests, then grab that Microsoft.Http.dll.
I'm not sure about a REST solution for 2.0. So I think WCF is going to be the way you'll have to go with this one. MSDN has a long introduction to it.
My personal philosophy is that if a technology is still in beta then I would rather not use it for something that will potentially be a production application so I would start with WCF since it was introduced in the 3.0 version of the framework.
3.0 and 3.5 is actually an update to the libraries, but the CLR is still 2.0 so if the libraries you're using are not deprecated or changed in the new frameworks it should be okay.
Take a look at this CodeProject for a starting point. It seems somewhat... hacky. Modernizing to WCF would be cleaner.
REST, or RESTFUL
Adding this to the System.Web in web.config will enable http get, and put.. aka REST-Like... but not full rest.
<System.Web>
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
</protocols>
</webServices>
</System.Web>
Related
In short, i am looking for the best mehod to provide a REST or SOAP API Server in a .Net Framework application (e.g. windows forms) - without admin rights in some cases
What is currently the best way of providing a web based REST or SOAP API in a possible portable csharp application?
Basically i need something that supports the basic http standards out of the box (e.g. Expect: 100-continue and others) and at the same time is able to instanciate the classes of my csharp program directly (perfomrance and ease of use reasons).
The microsoft way is to either use IIS and possibly ASP or go for httplistener. IIS could never be run in a portable way and requires lots of installation procedure/system administration based work. httlistener on the other hand is not even close to being a webserver, i would need to implement all the standard webserver commands on my own.
I am looking around for this topic since years now, one example is this question [old question] Alternative to HttpListener?
Unfortunately this one links to a discontinued project.
Any ideas?
[EDIT] The question targets not only C# but also .NET Framework 2-4.5. The result should be useable in e.g. Windows Form, Windows Service and Commandline applications.
Currently i am using a skeleton Webserver based on HTTPListener and therefore i need to implement all the Parsing of a request, formatting of answers and reacting to special http commands on my own (which seems to be a never ending task): https://www.codeproject.com/Articles/17071/Sample-HTTP-Server-Skeleton-in-C
You could try Griffin web server. I've used it for embedding a web server into applications to host a simple web interface, file hosting, and to provide a REST API for my application.
The biggest advantage for me versus the embedio project (which is excellent) is that it doesn't require admin privileges to run. Looks like no SOAP integration out of the box though.
You should be able to do what you want using .NET Core. You can fairly easily build a self-hosted API using it that's independent of IIS. Tutorials should be easy to find, and here is a Microsoft example.
As ilikesleeping suggested you could use dotnet core, but there are complications in making it work as a service.
I suggest you to use Microsoft OWIN framework. It's really simple and straightforward way of building restful applications. It can work fine as Console or a service, and of course in Console mkode you can display a Form should you wish to.
Here are some links to get kickstarted:
https://learn.microsoft.com/en-us/aspnet/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api | https://learn.microsoft.com/en-us/aspnet/aspnet/overview/owin-and-katana/getting-started-with-owin-and-katana | https://blog.decayingcode.com/post/Creating-a-Self-Hosted-OWIN-Application/ |
https://weblogs.asp.net/fredriknormen/creating-a-simple-rest-like-service-with-owin-open-web-server-interface
EDIT:
...and here's the topic on how to have a middleware that hosts SOAP endpoint over OWIN: Any way to get OWIN to host a SOAP service?
I am the author of this question. Just wanted to make obvious for future readers what i learned here:
Most interesting about this question is that it is a "shopping" question. The accepted answer cannot be based on facts but on subjective feeling only. Most of the suggested methods hit the described usecase.
This is the reason why some users did not want to write an answer but instead put their suggestions in a comment instead. Strange but this is how SO works. We just prefer scientifically correct answers here!
By te way, this was my first "bounty" question. I am active SO user since about 3 weeks. (passive for years, like most people)
I'm working on an application that requires me to host a WCF REST Web App using Windows Service. Now since it is going to be a bit more complicated than just a small API, I would like leverage MVC capabilities as a lot of things would be lot easier if done using the 'Controller' way (if I can say so). I'm don't have deep knowledge of MVC and Windows Service.
So far, I've found only two links here & here, that are somewhat related to what I want, but still not there. Could anyone please point me to a working example or create a small demo? TIA.
Based on your comments, it is clear that you are wanting to run REST-based web-services, self-hosted, on both Linux and Windows.
The recommended way to do this is to use the new ASP.NET Core platform, running on .NET Core.
Microsoft provide a good tutorial here:
https://docs.asp.net/en/latest/tutorials/first-web-api.html
Another link just received from a quick google search Here but yes need some more clarity on how you plan to run it or what its for.
We used Nugent package manager years ago for self-hosting a web API
Does anyone have experience using ASP.NET MVC project as a Web Service?
i.e. using ASP.NET MVC without Views, so other applications can use the URL to GET or POST to the actions in the Controller.
Has anyone used it? If so, are there any drawbacks for not using Web Service project instead?
Thank you all in advance!
It really depends on the kind of application you're writing. I would actually argue the inverse of LukLed's position - SOAP-based services are better suited for internal clients when you want to support things like Windows Authentication, or different protocols like TCP or MSMQ.
Using a more web-style of GETs and POSTs around specific "resources" starts to get you into the REST architectural style. This technique has a few distinct advantages to me:
The response is usually smaller, particularly when using lightweight formats like JSON
Because of the simplicity of the requests and responses, this makes it much easier to use in mobile / native applications (see Twitter's API, for example)
The service you create can be self-describing and discoverable, because you can link to other parts of your API just like normal web pages.
One article that particularly helped me understand the tradeoffs here is Martin Fowler's "Steps Toward the Glory of REST." That being said, it may or may not be the right fit for your application.
If you do choose to build a more REST-based service, definitely consider using the ASP.NET Web API built into MVC4 as others have mentioned. It's currently in beta, but Microsoft felt good enough about it to give it a go-live license.
UPDATE:
Since ASP.NET core, ASP.NET web API has been integrated into MVC 6 project.
https://wildermuth.com/2016/05/10/Writing-API-Controllers-in-ASP-NET-MVC-6
If you want to use simple GET and POST calls, MVC will be good choice. ASP.NET MVC 4 will have support for creating HTTP based APIs. You can read about it here: http://www.asp.net/web-api
Web Service created in Web Service project can be easier to consume, because it can generate WSDL file, that can be easily read and used in many different languages (by using SOAP protocol). On the other side, WS can create huge XML responses, that could be many times smaller, if you used your own format.
If you want to spread your web service around the world, allowing SOAP will make life easier for many developers. SOAP can be used by people, who almost have no idea about programming. If you'll use it internally, prefer speed and simple requests and responses, you can use MVC.
New ASP.NET MVC includes Web Api Kit, which can do exactly what you want. With current version you can still use it. There are no real drawbacks to it
I am looking to get into doing some iOS development for a nice addition to a project i am running.
The main project is currently written in C# and is mainly asp.net with a few windows services.
I would like to incorporate this to be able to develop a basic iPhone app as a proof of concept.
From what i have read and understand, its generally best practice to use JSON as a communication medium for iOS.
I am thinking about using WCF to create the API methods so the iOS app can connect to these services to get the data.
Are there any nice tutorials to do this?
Take a look at:
Developing RESTful iOS Apps with RestKit
I do this all the time. WCF Data Services (OData) are the way to go. With OData services, you can specify that you want JSON response by passing (Accept - Application/JSON) in the HTTP Header and OData will return JSON to you.
I have used several libraries for getting OData (which are REST services) from iOS. Microsoft's iOS OData implementation is pretty lame. RESTKit does a really good job for what it handles, but is really painful if you have to do something that it doesn't. I have also used ASI - it is much more flexible than RESTKit, but is not without problems. I ended up writing my own and it suits me just fine.
For a beginner, I would recommend using ASI over RESTKit. RESTKit, while doing a lot of the heavy lifting for you, takes a bit to get working right.
There are two things that are not standard when receiving JSON responses from OData.
1. All responses are captured in a JSONDictionary with the key of "D".
2. Dates are serialized to the JSON standard (number of seconds since 1970), but they are placed in a string like so: /Date(1212353), so you will have to parse out the Date() part of the string before you can use it.
RESTKit doesn't handle either one of these issues natively, so you will have to deal with them if you choose that route. Personally, I would go the ASI route until you learn enough to write your own.
I am considering open sourcing my solution - if I do, I will update this response with the link to it.
---UPDATE----
Just to be clear - if your server side system uses WCF Data Services, otherwise known as OData, then with minor tweaking, RESTKit plays nice with it. If your services are traditional WCF Services (i.e. SOAP Binding), then you will not be able to get JSON out of them because they are bound to the SOAP protocol (Unless you create a custom Behavior to translate it - which I wouldn't do). It all depends on what your services do in essence. If your services are typically data exposure/manipulation (i.e. addCustomer), then you should expose them as OData. If they are truly operations, then you should maybe consider exposing them as actions from a MVC site. Either of them can get you REST services using your existing infrastructure and platform.
If you're using Objective-C to develop the iPhone app, I'm not sure WCF is the best web service technology to use on the server. Check out ServiceStack to create a RESTful service.
Refer following link:
http://www.kotancode.com/2011/04/26/backing-your-ios-app-with-wcf-json-web-services/
It has included comprehensive code samples as well.
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?
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.
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";
}
}
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
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
Thje Shoulders Of Giants solution is now available via CodePlex and NuGet... renamed as Resources Over MVC.