Service Endpoint

Jan 30 2008   6:26AM GMT

Hello world web programming model



Posted by: Dilip Krishnan
SOA

Signed up for the Remember the milk service just to try and get a feel for their REST api’s, which are modelled just like Flickr’s API’s. What better method to call on a service than the quintessential “Hello World“. Turns out its very straight forward with the new Web Programming Model in .net 3.5 WCF

    [ServiceContract]
    
interface IRTMEchoService
    {
        [OperationContract]
        [WebGet(UriTemplate 
“services/rest/?method=rtm.test.echo&api_key={apiKey}”, BodyStyle WebMessageBodyStyle.Bare)]
        XElement Echo(
string apiKey);
    
}

All we did there was to create an interface to mimic the REST api defined for the echo method

    public class TestRTMEcho
    {
        
private string ApiKey “xxxxxxxxxxxxxxxxxx”;
        
[Test]
        
public void TestRtmEcho()
        {
            
using (WebChannelFactory<IRTMEchoService> factory = new WebChannelFactory<IRTMEchoService>())
            {                
                factory.Endpoint.Binding 
= new WebHttpBinding();
                
factory.Endpoint.Address = new EndpointAddress(new Uri(“http://api.rememberthemilk.com/”));
                
factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
                
IRTMEchoService task factory.CreateChannel();
                
Console.WriteLine(task.Echo(ApiKey));               
            
}
              
        }
    }

Wrote a small test that creates a WebChannelFactory and loads it with the default WebHttpBinding and WebHttpBehaviour point it to our endpoint and voila! the output as expected!

<rsp stat=”ok”>
  
<api_key>xxxxxxxxxxxxxxxxxxxxx</api_key>
  
<method>rtm.test.echo</method>
</rsp>

Notice the use of the UriTemplate property in the WebGet attribute in the IRTMEchoService interface.

Comment on this Post


You must be logged-in to post a comment. Log-in/Register