I am developing a WCF resful service which will be basically consumed from some mobile applications. Over the POST I am trying to send a DataContract object [actually I have to send a List of object] and another single id as string. My question is if it is possibly to define my function to accept DataContract object and the single string ?
Following is my code : Interface declaration:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetDataUsingDataContract/{id}")]
CompositeType GetDataUsingDataContract(string id, CompositeType composite );
}
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
Actual definition of the function:
public CompositeType GetDataUsingDataContract(string id, CompositeType composite )
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite .BoolValue)
{
composite .StringValue += "- Suffix and the id is"+id;
}
return report;
}
and the json object I am trying to send from Fiddler is
{"BoolValue":true,"StringValue":"sdfsdfsf"}
Above are the snaps from the fiddler I am testing the service from.
After couple of googling I have got the following link where the client actually uses webservice reference to get the DataContract type and serializes to json before sending as request body. But why then my test from Fiddler doesn't succeed ?!
https://geeksarray.com/blog/wcf-rest-service-to-get-or-post-json-data-and-retrieve-json-data-with-datacontract
Can anybody please suggest anything ?
The web.config is as bellow:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="JSONWebService.Service1" behaviorConfiguration="JSONWebService.Service1Behavior">
<endpoint address="../Service1.svc"
binding="webHttpBinding"
contract="JSONWebService.IService1"
behaviorConfiguration="webBehaviour" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="JSONWebService.Service1Behavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>