简单的 WCF 服务

WCF 服务的最低要求是一个具有一个 OperationContract 的 ServiceContract。

服务合约:

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

服务合同实施:

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

配置文件:

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