I'm new to WCF (and pretty rusty with .NET in general) so there's a distinct chance this is an answered question and I just missed it.
I'm building an ASP.NET MVC app that will be using a RESTful JSON-based API for its backend. I've been looking into the different options for how to talk to such an API in .NET and it looks like WCF is the most popular choice by far. Reading into WCF some more I now have a basic consumer class that makes a request, which is a start.
But now I need to do more with it, and I'm not making much headway. I need to send a POST to the API with a JSON body. Here's what I have so far:
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Runtime.Serialization;
namespace APIConsumer {
[ServiceContract]
public interface IAPIClient {
[OperationContract]
[WebInvoke(
Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/session/login.json"
)]
string SessionLogin(string login_name);
}
public class APIClient : ClientBase<IAPIClient>, IAPIClient {
public string SessionLogin(string login_name) {
return this.Channel.SessionLogin(login_name);
}
}
}
What I can't figure out is the correct way to pass a) ANY data within the POST body & b) a proper serialized .NET object as JSON in the POST body. Is there a good example of how to work that out there somewhere?