Démarrer avec wcf

Démonstration du service reposant WCF

svc

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

Interface

[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 (clic droit sur le fichier svc et cliquez sur afficher le balisage)

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

Configuration 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>

Maintenant, exécutez simplement le service ou l’hôte dans un port. Et accédez au service en utilisant “http://hostname/WCFRestfulService/GetServiceName?Id=1”

Service WCF simple

Les exigences minimales pour le service WCF sont un ServiceContract avec un OperationContract.

Contrat de service:

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

Mise en œuvre du contrat de service :

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

Fichier de configuration:

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