wcf Getting started with wcf Simple WCF service


Example

Minimal requirements for WCF service is one ServiceContract with one OperationContract.

Service contract:

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

Service contract implementation:

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

Configuration file:

<?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; }
}