dimanche 26 avril 2015

How to call a microservice in .NET


I've created a very simple REST microservice that receives information about an email and sends it. The microservice send method looks something like this:

//EmailController
[HttpPost]
public IHttpActionResult Send(Email email)
{
    // send email via exchange
}

Now in my application, I call it using RestSharp like this:

var client = new RestClient("http://localhost:51467/api/");
var request = new RestRequest("email/send", Method.POST);
request.RequestFormat = DataFormat.Json;
dynamic obj = new ExpandoObject();
obj.FromAddress = from;
obj.ToAddress = to;
obj.Subject = subject;
obj.Body = body;

request.AddBody(obj);
client.Execute(request);

Questions I have:

  1. Is this the best way to do the call? Obviously i'll later have to add error handling etc, but I'm talking more the way I'm using RestSharp to do the call.

  2. I'm finding it a bit uncomfortable that my app needs to kind of know what object the microservice expects to receive - there's no sort of definition/interface/contract that it uses to know for sure. Is this generally accepted as being ok for REST or should I implement some sort of interface that my app has so it can call my microservice in a bit more of a defined way. Is that even something possible with REST?

Thanks for any help!


Aucun commentaire:

Enregistrer un commentaire