I usually donβt worry about additional client libraries such as RestSharp. I feel that the goal of REST is to get as close to the golden old HTTP as possible, denying the need for anything other than HttpWebRequest / Response. Working with requests / answers directly gives excellent control and encourages you to think about what is actually happening, instead of abstracting from everything, as with the traditional WCF or ASMX service.
For the microservices that I built in the past, I saved the request and response objects in separate libraries, and I distributed the source to other developers in my organization to give them support for calling the service, but this probably will not be practical for external consumers; again, I assume that the point of transition to the microservice for the full-scale WCF service is that by nature, the transmitted requests / responses are small and simple. I also felt a little uncomfortable with this practice in the beginning; however, when I started getting really responsive web applications that call microservices with javascript (usually jquery) as easy as traditional .NET, I began to see the potential for really good integration of our internal systems. Ultimately, our intranets provided actions and views in business applications that were previously not possible.
HttpWebRequest webRequest = WebRequest.Create("http://localhost:51467/api/email/send") as HttpWebRequest; webRequest.Method = "POST"; webRequest.Credentials = CredentialCache.DefaultCredentials; //or account you wish to connect as webRequest.PreAuthenticate = true; webRequest.ContentType = "application/json"; // or xml if it your preference string jsonData = Newtonsoft.Json.JsonConvert.SerializeObject(requestObject); using (StreamWriter streamWriter = new StreamWriter(webRequest.GetRequestStream())) { streamWriter.Write(jsonData); streamWriter.Flush(); streamWriter.Close(); } HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse; if (webResponse.StatusCode != HttpStatusCode.Accepted) throw new ApplicationException("Unexpected Response Code. - " + webResponse.StatusCode); string response; using (System.IO.StreamReader readResponse = new System.IO.StreamReader(webResponse.GetResponseStream())) { response = readResponse.ReadToEnd(); } //swap out for regular xml serializer if you've used xml dynamic responseObject = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(response);
One more tip if you work with web api, I would really suggest adding a web api and a test client to the help pages. You wonβt have the automatically created wsdl that you get with WCF and ASMX, but you can get really good documentation about your microservice for other developers (even better, in my opinion, auto-generated proxy classes) and a test harness that allows you to use services from your browser
https://github.com/wuchang/WebApiTestClient https://www.nuget.org/packages/Microsoft.AspNet.WebApi.HelpPage/
source share