.Net - Self Host WCF In JSON Format

1. Data Contract

using System.Runtime.Serialization;
namespace JohnsonLau.RemoteControl.DataContracts
{
    [DataContract]
    public class Command
    {
        [DataMember(IsRequired = true, Name = "name")]
        public string Name
        {
            get;
            set;
        }
        [DataMember(IsRequired = false, Name = "value")]
        public string Value
        {
            get;
            set;
        }
        [DataMember(IsRequired = true, Name = "user")]
        public string User
        {
            get;
            set;
        }
        [DataMember(IsRequired = true, Name = "password")]
        public string Password
        {
            get;
            set;
        }
    }
}
using System.Runtime.Serialization;
namespace JohnsonLau.RemoteControl.DataContracts
{
    [DataContract]
    public class Response
    {
        [DataMember(Name = "success")]
        public bool Success
        {
            get;
            set;
        }
        [DataMember(Name = "message")]
        public string Message
        {
            get;
            set;
        }
    }
}

2. Service Contract

using JohnsonLau.RemoteControl.DataContracts;
namespace JohnsonLau.RemoteControl.Services
{
    [ServiceContract(Namespace = "http://RemoteControl.JohnsonLau.Net")]
    public interface IControlService
    {
        [OperationContract]
        [WebInvoke(
            Method = "POST",
            UriTemplate = "SendCmd",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json)]
        Response SendCmd(Command cmd);
    }
}

3. Service

using JohnsonLau.RemoteControl.DataContracts;
namespace JohnsonLau.RemoteControl.Services
{
    public class ControlService : IControlService
    {
        public Response SendCmd(Command cmd)
        {
            string msg = "success";
            return new Response()
            {
                Success = true,
                Message = msg
            };
        }
    }
}

4. app.config

<?xml version="1.0"?>
<configuration>
    <system.serviceModel> 
        <services> 
            <service name="JohnsonLau.RemoteControl.Services.ControlService"> 
                <host> 
                    <baseAddresses> <add baseAddress="http://localhost:8001/RemoteControlService"/> </baseAddresses> 
                </host> 
                <endpoint address="" behaviorConfiguration="httpBehavior" binding="webHttpBinding" contract="JohnsonLau.RemoteControl.Services.IControlService" /> 
            </service> 
        </services> 
        <behaviors> 
            <endpointBehaviors> 
                <behavior name="httpBehavior"> <webHttp /> </behavior> 
            </endpointBehaviors> 
            <serviceBehaviors> 
                <behavior name=""> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> 
            </serviceBehaviors> 
        </behaviors> 
    </system.serviceModel> 
    <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> 
</configuration>

5. Start host

ServiceHost mServiceHost= new ServiceHost(typeof(ControlService));
mServiceHost.Open();
mServiceHost.Close();