Look at this code
My server code as below
[ServiceContract]
public interface IWinPhoneService
{
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "getkpimeasuredata")]
List<String> GetKpiMeasureData(DomainData data);
}
[DataContract]
public class DomainData
{
[DataMember]
public int? KPIId { get; set; }
[DataMember]
public int? ScorecardId { get; set; }
[DataMember]
public short? CumulativeMonth { get; set; }
[DataMember]
public int? EngineeringOrgId { get; set; }
[DataMember]
public int? BusinessOrgId { get; set; }
[DataMember]
public int? DataValuetypeId { get; set; }
}
Now my implementation of the interface as below
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class WinPhoneService : IWinPhoneService
{
public List<string> GetKpiMeasureData(DomainData data)
{
if (data != null)
{
return new List<string>() {"Sample 1","Sample 2"};
}
return new List<string>() {"No data recieved"};
}
}
My server side web.config for endpoint element configuration
<system.serviceModel>
<services>
<service name="WinPhoneService.WinPhoneService">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" bindingConfiguration="defaultRestJsonp" contract="WinPhoneService.IWinPhoneService">
</endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="defaultRestJsonp" crossDomainScriptAccessEnabled="true">
<readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="64" maxNameTableCharCount="2147483647" />
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
Now with all the above set find my client code below using RestSharpClient:
private static string ToStringUsingDataContractJsonSer<T>(T requestBody)
{
var serializer = new DataContractJsonSerializer(typeof(T));
var ms = new MemoryStream();
serializer.WriteObject(ms, requestBody);
ms.Position = 0;
var reader = new StreamReader(ms);
return reader.ReadToEnd();
}
private string UseRestSharpApproach(string serviceBaseUrl, string resourceUrl, Method method, object requestBody)
{
var client = new RestClient();
client.BaseUrl = serviceBaseUrl;
var request = new RestRequest(method) { DateFormat = DataFormat.Xml.ToString(), Resource = resourceUrl };
request.AddParameter("application/json", requestBody, ParameterType.RequestBody);
var response = client.Execute(request);
string responseString;
if (response.StatusCode == HttpStatusCode.OK)
{
responseString = HttpUtility.HtmlDecode(response.Content);
}
else
{
responseString = response.StatusDescription + " --------------------" + HttpUtility.HtmlDecode(response.Content);
}
return responseString;
}
public void CallRESTService()
{
UseRestSharpApproach(serviceBaseUrl, resourceUrl, Method.POST, ToStringUsingDataContractJsonSer<DomainData>(objDomainData));
}
Make sure that the DomainData object when using on the client side is in the same namespace as on server so that the serialization and de-serialization happens without problems.
Now my raw request from Fiddler looks as below:
POST http://servername/Sample/WinPhoneService.svc/GetKpiMeasureData HTTP/1.1
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
User-Agent: RestSharp 102.0.0.0
Content-Type: application/json
Host: servername
Content-Length: 112
Accept-Encoding: gzip, deflate
{"BusinessOrgId":1,"CumulativeMonth":463,"DataValuetypeId":1,"EngineeringOrgId":11,"KPIId":1006,"ScorecardId":3}
And the response looks as below:
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 23
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS
X-AspNet-Version: 4.0.30319
Set-Cookie: ASP.NET_SessionId=123450p12rvdmnh3qrx2ocip; path=/; HttpOnly
Date: Tue, 03 Sep 2013 10:11:51 GMT
["Sample 1","Sample 2"]