Empezando con wcf

Demostración del servicio tranquilo de WCF

servicio

 public class WCFRestfulService : IWCFRestfulService 
    {
       public string GetServiceName(int Id)
        {
            return "This is a WCF Restful Service";
        }
    }

Interfaz

[ServiceContract(Name = "WCRestfulService ")]
public interface IWCFRestfulService 
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetServiceName?Id={Id}")]
    string GetServiceName(int Id);
}

svc Markup (haga clic con el botón derecho en el archivo svc y haga clic en ver MarkUp)

<%@ ServiceHost Language="C#" Debug="true" Service="NamespaceName.WCFRestfulService" CodeBehind="WCFRestfulService.svc.cs" %>

Configuración web

<services>
      <service name="NamespaceName.WCFRestfulService" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="NamespaceName.IWCFRestfulService" behaviorConfiguration="web"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- 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, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

Ahora simplemente ejecute el servicio o host en un puerto. Y acceda al servicio usando “http://hostname/WCFRestfulService/GetServiceName?Id=1”

Servicio WCF simple

Los requisitos mínimos para el servicio WCF son un ServiceContract con un OperationContract.

Contrato de servicios:

[ServiceContract]
public interface IDemoService
{
    [OperationContract]
    CompositeType SampleMethod();
} 

Implementación del contrato de servicio:

public class DemoService : IDemoService
{
    public CompositeType SampleMethod()
    {
        return new CompositeType { Value = "foo", Quantity = 3 };
    }
}

Archivo de configuración:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

DTO:

[DataContract]
public class CompositeType
{
    [DataMember]
    public string Value { get; set; }

    [DataMember]
    public int Quantity { get; set; }
}