Test entity classes: (entities with the same field name need to be built on the client and service side)
public class Compositetype
{
Public Compositetype ()
{
Subcompositetypes = new List<subcompositetype> () {New Subcompositetype ()};
}
public bool Boolvalue {get; set;}
public string StringValue {get; set;}
Public list<subcompositetype> Subcompositetypes
{
Get
Set
}
}
public class Subcompositetype
{
bool Boolvalue = true;
String stringvalue = "Hello";
public bool Boolvalue
{
get {return boolvalue;}
set {boolvalue = value;}
}
public string StringValue
{
get {return stringvalue;}
set {stringvalue = value;}
}
}
Client Request Code:
#region Josnpost
Compositetype Compositetype = new Compositetype
{
StringValue = "1",
Boolvalue = False
};
DataContractJsonSerializer Dcserializer = new DataContractJsonSerializer (typeof (Compositetype));
MemoryStream stream = new MemoryStream ();
Dcserializer.writeobject (stream, Compositetype);
String data = Encoding.UTF8.GetString (stream. ToArray (), 0, (int) stream. Length);
HttpClient client = new HttpClient ();
Client. DEFAULTREQUESTHEADERS.ACCEPT.ADD (New Mediatypewithqualityheadervalue ("Application/json"));
String link = "Http://localhost:1766/Service1.svc/CreateUser";
Httpresponsemessage Respondse = await client. Postasync (link, new stringcontent (data));
String Datawithjason = await Respondse. Content.readasstringasync ();
var request = (HttpWebRequest) webrequest.create (new Uri (link));
Request. ContentType = "Application/json";
Request. method = "POST";
using (var requeststream = await request. Getrequeststreamasync ())
{
var writer = new StreamWriter (requeststream);
Writer. Write (data);
Writer. Flush ();
}
using (var resp = await request. Getresponseasync ())
{
using (var responsestream = resp. GetResponseStream ())
{
var reader = new StreamReader (responsestream);
var result = reader. ReadToEnd ();
}
}
Service-side Interface definition:
[OperationContract]
[WebInvoke (
UriTemplate = "CreateUser",
Requestformat = Webmessageformat.json,
Responseformat = Webmessageformat.json,
method = "POST")]
String CreateUser (Compositetype compositetype);
Service-side Interface implementation:
public string CreateUser (Compositetype compositetype)
{
Return "OK" + Compositetype.stringvalue + "" + Compositetype.boolvalue + "" + CompositeType.SubCompositeTypes.FirstOrDef Ault (). StringValue;
}
Because the server needs to implement rest services, it needs to be reconfigured in Web.config (cover ServiceModel):
<system.serviceModel>
<services>
<service behaviorconfiguration= "Myservicebehavior" name= "Server.service1" >
<endpoint address= "" binding= "WebHttpBinding" behaviorconfiguration= "web" contract= "Server.iservice1" >
<identity>
<dns value= "localhost"/>
</identity>
</endpoint>
<endpoint address= "Max" binding= "mexHttpBinding" contract= "IMetadataExchange" ></endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name = "Web" >
<webhttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name= "Myservicebehavior" >
<!--to avoid disclosing metadata information, set the values below to false before deployment
<servicemetadata httpgetenabled= "true" httpsgetenabled= "true"/>
<!--to receive exception details in faults for debugging purposes and set the value below to true. Set to false before deployment to avoid disclosing exception information
<servicedebug includeexceptiondetailinfaults= "false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding= "basichttpsbinding" scheme= "https"/>
</protocolMapping>
<servicehostingenvironment aspnetcompatibilityenabled= "true" multiplesitebindingsenabled= "true"/>
</system.serviceModel>
Tested, also available in Windows Runtime projects.