Category Archives: Refit

Trying out Refit (a REST client library)

REST is cool in that it’s lightweight and simple to use but lacks a lot of the tooling etc. that SOAP/XML has.

In .NET we have libraries such as ServiceStack (and others) to enable generation of REST services to be fairly painless, there are also a few client frameworks out there, but for this post I’m looking at Refit which (as the documentation states) is automatically type-safe and available in .NET standard 1.4 which also includes Xamarin, UWP and desktop. So pretty universal in it’s availability.

Show me the code!

Enough chat, let’s get into the code. I’m going to use ServiceStack to generate a simply REST service, the default one is the “Hello” service, which in code looks like this

[Route("/hello/{Name}")]
public class Hello : IReturn<HelloResponse>
{
   public string Name { get; set; }
}

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

and this will be called using http://127.0.0.1:8088/hello/World which in turn returns (in JSON format)

{"Result":"Hello, World!"}

All pretty simple.

One thing we will do, is switch ServiceStack to use JSON be default, so in the MyService generated code we’ll add an AddHeader attribute, so it will look like this

public class MyServices : Service
{
   [AddHeader(ContentType = MimeTypes.Json)]
   public object Any(Hello request)
   {
      return new HelloResponse { Result = "Hello, {0}!".Fmt(request.Name) };
   }
}

Creating our client

Fire up Visual Studio and create a console application (I’ve targeted .NET 4.6), then add Refit via NuGet.

As we already have the HelloResponse class from our ServiceStack sample application, we can just copy that into our Refit application, or visit https://quicktype.io/ and paste {“Result”:”Hello, World!”} into the JSON edit box on the left of the site and it’ll create the class for us. So using either method we need to add the JsonProperty so it now looks like this in our Refit app.

public class HelloResponse
{
   [JsonProperty("Result")]
   public string Result { get; set; }
}

Next up we need create an interface that calls our REST service. We know, having written/generated the code for the service that there’s a Hello class that defines the route and response, but we won’t be copying this, instead Refit likes to use methods returning a Task (which makes sense being a web service call) so create (in your Refit app.) the following

public interface IHelloService
{
   [Get("/hello/{name}")]
   Task<HelloResponse> SayHello(string name);
}

Note we create an interface and that the route is within the Get attribute points to our Hello endpoint. Next we created a method to be called to contact our endpoint at the aforementioned route. This is the main area where we actually have to do any coding for this example to work.

Let’s now create the client code

var svc = RestService.For<IHelloService>("http://127.0.0.1:8088");
var response = svc.SayHello("World");

and that’s it – obviously you’ll need to await or Result etc. to get the actually HelloResponse object, but that’s all there is to this (admittedly very simple example).

As you can see the RestService.For method creates an instance of an IHelloService for us acting as a transparent proxy through to the service.

References

The automatic type-safe REST library for Xamarin and .NET
Exploring refit, an automatic type-safe REST library for .NET Standard