tags:

views:

209

answers:

4

I am specifically looking for an example which use a) WCF & REST. After a long googling, though I got some but they are beyond my understanding.

Could some one please give me a very simple example say "Hallow World" or Summation of 2 numbers which will give me a clear insight about how to write a server, and also how to consume the same from the client end.

Also if any good link that explains this kind of example in simple terms kindly tell me that.

Thanks

A: 

You can create an WCF REST web service by configuring your endpoint to use a webHttpBinding as seen in this in-depth tutorial:

http://www.west-wind.com/weblog/posts/310747.aspx

Here is another open source web services framework that simplifies creating XML and JSON REST web services without requiring any extra configuration.

Edit: Added link to good article articulating the spirit of REST:

http://tomayko.com/writings/rest-to-my-wife

Link to blog post comment explaining common misconceptions of REST:

http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

mythz
+1 for the suggestion looking at alternatives to WCF for REST...
The suggested alternative framework seems to think that returning xml and Json instead of soap messages qualifies it as Restful. This is a common misconception.
Darrel Miller
I mean that it supports REST-full urls for all web services, i.e. you can call each web service using a simple GET request, e.g. /GetFactorial?ForNumber=2 on both the Xml and Json endpoints. It also supports calling endpoints with the different HTTP Verbs i.e. GET, POST, PUT, DELETE. Though it's still up to the web service creator to implement it as such.
mythz
Darrel Miller
Your just being pedantic. If you look at the definition of REST-full web services in http://en.wikipedia.org/wiki/Representational_State_Transfer 'A RESTful web service is a simple web service implemented using HTTP and the principles of REST... 1) The base URI for the web service 2) The MIME type of the data supported by the web service. often JSON, XML or YAML 3) The set of operations supported by the web service using HTTP methods (POST, GET, PUT or DELETE).' The web service framework enables you to do each of these, although admit-tingly its up to developer to adhere to the spirit of REST.
mythz
For those interested, a good article articulating the spirit of REST is here: http://tomayko.com/writings/rest-to-my-wife
mythz
@Darrel looking at your history I can see you have an array of knowledge developing 'REST-based resources' (or what ever you like to call it), I just don't agree with your opinion that there is no such thing as ReSTful Url or ReSTful web services.
mythz
I understand why you think that I am just being pedantic. The problem is that a rest based system is supposed to exhibit certain beneficial characteristics. If the constraints are not adhered to, it will not. My fear is that in 3-4 years we have an uprising where people start to say, hey this ReST thing is no better than anything else we have tried. And a few of us will be jumping up and down saying "because what you were doing was not ReST", but we will not be heard and ReST will be branded as another failed methodology.
Darrel Miller
This http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven is what you need to understand to determine if something is a REST api. It is critical to understand that it is a perfectly valid choice to not meet the REST constraints. A number of people are trying to develop nomenclature for web services that are HTTP based but on meet a subset of the REST constraints. Hopefully this will help to clear up the issues.
Darrel Miller
@Darrel fair call, maybe it's only a partial implementation then. Though I can't really see how ReST-full web services can be decoupled from HTTP and still work. If you don't specify the endpoint type in the 'uri' then you can only support one 'mime-type' unless you force the clients to specify it in the HTTP request header - which I don't think is very pragmatic as it becomes impossible to send a link to to someone to the 'XML resource data'.
mythz
From ReST's perspective an URL is an identifier. What it looks like is irrelevant, in the same way a procedure name is irrelevant to the compiler in a procedural language. If you are doing REST over http, which 99.999% of us are, then the URI will start with http:// If I have said anything that leads you to believe otherwise then I apologize. The mime-type returned or sent is completely orthogonal to the endpoint.
Darrel Miller
+1  A: 

REST in WCF is not that hard once you figure it out.

First you must define your interface.

Here is an example.

[ServiceContract]
public interface IRESTExample
{
    [WebGet(UriTemplate = "interaction/queue?s={site}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
    [OperationContract]
    string QueueInteraction(string site);

    [WebGet(UriTemplate = "interaction/cancel?id={interactionId}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
    [OperationContract]
    string CancelInteraction(string interactionId);

    [WebGet(UriTemplate = "queue/state?s={site}&q={queue}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
    [OperationContract]
    string QueueState(string site, string queue);

}

You can see in the WebGet you define the final URL. So it depends on where you bind it, but say you bind the endpoint to www.example.com/rest

QueueInteraciton would be www.example.com/rest/interaction/queue?s=SomeSite

Where {stie} or {parameterName} is replaced with the name of the parameter.

The implemetion is just a simple class, I am going to assume you know how to implement an interface. If you need help just leave a comment.

Now binding the endpoint. In the end it is not that hard, you can do it all in the config.

<system.serviceModel>
    <services>
        <service name="Stackoverflow.Example.Service.RestExample" behaviorConfiguration="MyServiceTypeBehaviors">
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:2136/RestExample"/&gt;
                </baseAddresses>
            </host>

            <endpoint address="rest" binding="webHttpBinding" behaviorConfiguration="xmlBehavior" contract="Stackoverflow.Example.Service.IRESTExample" />

        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MyServiceTypeBehaviors" >
                <!-- Add the following element to your service behavior configuration. -->
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
        </serviceBehaviors>

        <endpointBehaviors>
            <behavior name="jsonBehavior">
                <webHttp/>
            </behavior>
            <behavior name="xmlBehavior">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <bindings>
        <basicHttpBinding>
            <binding name = "NoSecurity">
                <security mode = "None" />
            </binding>
        </basicHttpBinding>
    </bindings>
</system.serviceModel>

Now the code to start the service and bind it. YOu can do it in anything, for example a console app.

RestExample exampleService = new RestExample();

host = new ServiceHost(exampleService);

host.Open();

This should be enough to get started.

David Basarab
Your example breaks the most fundamental constraint that ReST defines. You are using the GET method for unsafe operations, i.e. the Queue and Cancel operation.
Darrel Miller
A: 

msdn article An Introduction To RESTful Services With WCF with code example at msdn code gallery. Also check out this codeproject article

the empirical programmer
+1  A: 

If you really want to do ReST then use a web framework that will lead you down the right path. See OpenRasta.

It is not impossible to do WCF to do ReST, it is just very difficult to learn how to do Rest with a framework that will frequently get in your way and lead you in the wrong direction.

Darrel Miller