I am bit new to WCF services. In my current, I have develop a hello world Restful WCF service. The following is the code of my RESTful web service.
The RESTful service contract is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace RestfulWCFService
{
[ServiceContract]
public interface IRestfulTestService
{
[OperationContract]
[WebInvoke(Method="GET", ResponseFormat= WebMessageFormat.Xml, UriTemplate="xml/{name}")]
string SayHelloXml(string name);
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "json/{name}")]
string SayHelloJson(string name);
}
}
The interface implementation is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace RestfulWCFService
{
public class RestfulTestService : IRestfulTestService
{
string IRestfulTestService.SayHelloXml(string name)
{
return "Hello " + name;
}
string IRestfulTestService.SayHelloJson(string name)
{
return "Hello " + name;
}
}
}
I have deployed this service on IIS and now I am accessing this web service using the following URL.
http://localhost/RestfulWCFService/RestfulTestService.svc/xml/pankesh
The webservice is returning me the following data.
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hello pankesh</string>
Now, my question is - on the above webservice, I am passing a simple data type that is STRING. Now, my requirement is I want to pass a complex custom object through REST request. Could you please advise me - how can I pass the custom object through a REST request on the above scenario?
The content of web.config
file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<services>
<service name="RestfulWCFService.RestfulTestService">
<endpoint behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="" contract="RestfulWCFService.IRestfulTestService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>