We have a homegrown framework that might be useful
to implement REST based webservices.
It is a .net c# project, used in a webapplication.
What it is used like: inline substitution of template 'tags' with dynamic content. sample tag: {{recentposts window=7 max=10}}
What it does: parsing 'tag' to command with (checked) parameters, invoking a
handler configured to handle the command and return data, transforming the data with xsl,
substitute {{...}} with the result.
I have a hunch that this could be reworked to create some form of REST based
services, parsing an url to a command with parameters, invoking a handler etc.
and writing the result to http response.
As an alternative to reworking I'm looking for smth
that might be useable instead, out of the box.
What are mature (open source) frameworks that could be used?
It has to provide a http facade, to do the REST stuff easily, and besides provide an API,
a way to bypass this facade, allowing command objects to be created, having all the invocation and transformation done and instead of writing to http response to some stream.
How about ServiceStack?
Quote from the webpage:
A modern, code-first, DTO-driven, WCF replacement web services framework encouraging best-practices for creating DRY, high-perfomance, scalable REST web services
...and an "overview" slideshow.
I use EasyHttp to work with REST base serices, it works easily with JSON and XML services and also supports working with retrieved object as a dynamic object. Very easy to plug and use and you don't have to worry about Http Request/Response anymore.
I think it may be worth taking a look at OpenRasta
https://github.com/openrasta/openrasta-stable/wiki
The OpenRasta project is a web framework that les you build web
applications as simple as
public class Home { public string Get() {
return "Hello world"; } }
It's really nice to use and easy to get started with
Related
I am working on the asp.net web api where i have to return back objects in json. With respect to making a better approach and easy for (android/ios)mobile developer to consume these Web APIs and parse json objects, what is best approach for making these objects definitions remain shared amongst webapi project and mobile project, so that if we have to change any property then it can easily be reflected on both projects in a better way. It would be great if someone explains it in detail.
There is no such sync method as you are asking.
On your WebAPI server side you'll define the objects and then return them in your API methods. JSON serialization will be automatically handled by the framework, using your serialization engine of choice (i.e. JSON.NET). Remember that with WebAPI you don't decide the output format server side, you just return a response containing the object(s) and then the framework reads the HTTP HEADERS of the request to determine whether the client asked for JSON or XML and then returns what was asked.
The best thing you can do is define a clear API with nice conventions and keep it documented, and if you change anything have the documentation reflect the changes. Avoid making breaking changes, and if you really must, deprecate a property or an object for at least a couple of versions before removing it.
That's the way all public API work anyway.
I need to create a backing service for a jQuery token input field control.
Our application consists of controls for a (third-party) DotNetNuke module called SimpleWrapper. The way this module works is that it provides a lightweight but not very flexible way of displaying regular ASP.NET user controls on a DNN page. The caveat is these are .ascx controls, not .aspx pages.
I'm mostly at a loss at which of the various technologies available to use. I looked at ASMX services but those mostly seem tailored to producing generated JavaScript proxy code. I need to be able to:
mount the service at a static URL
have it accept a single string parameter
have it produce JSON in a specific, but very simple format
I don't really need strong integration with ASP.NET, like being able to respond to a postback or some such. I'd also prefer something deployable just by adding a file, without having to edit configuration files. What would be a straightforward way to spit out a chunk of JSON in such an environment?
WCF (I think starting with version 3.51) has a nice "zero config" feature that integrates easily with IIS. All you have to do is
create a JSON aware interface & service
create a simple .SVC file in the IIS site.
You don't need to mess with funky .config files :-)
Example .SVC file:
<%# ServiceHost
Service="MyNamespace.MyService"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
Example interface & service implementation, something like this:
public class MyService : IMyService
{
public string Test(string text)
{
return text; // whatever
}
}
[ServiceContractAttribute(Namespace="http://schemas.myservice.com")]
public interface IMyService
{
[OperationContractAttribute]
[WebInvokeAttribute(UriTemplate="Test", // change this accordingly
ResponseFormat=WebMessageFormat.Json, // change this accordingly
RequestFormat=WebMessageFormat.Json, // change this accordingly
BodyStyle=Wrapped)]
string Test(string text);
}
Here is an extra cool link about all this: WCF Web Services The Easy Way.
JSON Exposed thru Restful Service
This Link will guide you step by step on how to do what I believe to be what you are looking for has actual code sample
I don't see why WCF Web services or ASMX Web Services are not suitable for what you say you need. Personally, that's the way I'd go (choosing WCF over ASMX).
I looked at the link DJ KRAZE posted and it uses an HTTP Handler plus uses some third party Javascript serializer (one extra dependency that isn't really needed since you have JavascriptSerializer if needed).
I'm in the process of trying out a few things with MVC whilst designing a system and wanted to try and see if I could use the concept of Controllers outside of the MVC framework. When I say outside, I mean within my own C# service as opposed to a web-site.
I started a simple console application to test the theory and it was simple enough to change the profile to a non-client profile, add in System.Web.Mvc, create a controller and have it return a JsonResult. The ease of which this got set up pleased me as that is half the work done if I want a service to respond with JSON.
The next step is to set up a Http Server class, and I would love it if I could leverage the other part of the framework which will map incoming requests to my controllers. Unfortunately, this is the part where I am lost and I have no idea what code goes on behind to arrive at a particular controller's function with the parameters all in place.
Has anyone got an idea on how to accomplish this, or a resource to look at?
In short: I'd like to leverage the use of Controllers in my own service, running it's own HTTP Server.
You can use the design pattern without using the framework - what I mean is, you can apply the model view controller pattern wherever you believe it solves the problem - if you think that you can replace "view" with "service", you could apply some of the concepts...
http://msdn.microsoft.com/en-us/library/ff649643.aspx
However, there are other patterns that may lend themselves better to services, although if we are talking specifically about a JSON service, then just using the ASP.NET MVC framework as it is would work well (rather than trying to re-write it).
Are you not trying to reinvent the wheel?
If returning JSON is one of your main purpose then WCF fulfills your need. Having WCF with you you are free to host it in IIS. This serves your second purpose having its own HTTP server.
You are trying to achieve some kind of routing where based on URL your different actions will be called. Isn't it similar to having a WCF service with different methods and client is calling each of them with different URL?
Trying controller concept in a non web application seems to be innovative, however in your case it looks like over-engineering.
The basic MVC pattern isn't all the difficult to replicate. I would seriously consider writing your own, rather than trying to shoehorn the MVC classes into your app.
Simon
If it helps you, the entire ASP.Net MVC Framework is open source, you can download it all from http://aspnet.codeplex.com/ . You can use the libraries here to view how the Framework is doing things behind the scenes and adapt things for your own use as appropriate.
I have an asp.net MVC2 application that needs to call a web service from the controller. How do I do this? It is a RESTful service that returns Json data.
I cant seem to find a good example.
Thanks
You call it the same way you would do in any ASP.net application, it is not connected to MVC at all.
Either add a reference and use that (easiest) or go with the manual method: here is a guide, see at towards the end (fig. 14 in particular) for consuming such services:
http://msdn.microsoft.com/en-us/magazine/dd943053.aspx
I have written my own ActictiveResource client framework, which allows the consumer to specifiy the http provider and the serialisation provider. The generic activeResource class has the four main verbs (get,put,post,delete) as methods which it calls against a specified resource url, passed in at cunstruction. the fololwing is an example of getting a product item from teh service:
ActiveResource<Product> arProduct = new ActiveResource<Product>(jsoSerializer,liveHttpProv,"https://company/product/1452");
//Get verb
Product prod = arProduct.Get();
Of course you can also use the other verbs on the object to put, post and delete.
arProduct.Post(prod);
The code, basically, wraps up the underlying http post,put, get functions and takes care of the serialiasation of the payload to objects. It has been a very useful component which I have used over and over again. The code can be easily called from a controller, it may be worth using a IOC container (I am using Th eUnity block) to instatiate you providers
Hope this helps
I would put together a simple class that acts as a "client" that makes a web-request from the URL, and then return the response as a string.
From there you can deserialize the JSON data using either the JSON serialization that ships with WCF, or the most excellent JSON.Net library. You will need to create a simple data class that is structured in the same way as the JSON data your expecting back.
You could also combine the two and have your client class return the deserialized object directly.
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.
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.
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.
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?
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).
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.
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.
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.
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.
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).
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.
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?
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.
ASP.Net MVC is naturally a Http based framework. It deals with HTTP verbs, media types, URLs, response headers and request headers.
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.
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 ;-)
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.
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.
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.