Partager via


Comment : implémenter un contrat de service Windows Communication Foundation

Il s'agit de la deuxième des six tâches requises pour créer un service Windows Communication Foundation (WCF) de base et un client pouvant appeler le service. Pour disposer d'une vue d'ensemble des six tâches, consultez la rubrique Didacticiel de mise en route.

La création d'un service WCF requiert au préalable la création du contrat, défini à l'aide d'une interface. Pour plus d'informations sur le sujet suivant la création de l'interface, consultez Comment : définir un contrat de service Windows Communication Foundation. L'étape suivante, affichée dans cet exemple, consiste à implémenter l'interface. Cela implique la création d'une classe appelée CalculatorService qui implémente l'interface ICalculator définie par l'utilisateur. Le code utilisé pour cette tâche est fourni dans l'exemple qui suit la procédure.

Pour implémenter un contrat de service WCF

  1. Créez une classe appelée CalculatorService dans le même fichier où vous avez défini l'interface ICalculator. CalculatorService implémente l'interface ICalculator.

    Public Class CalculatorService
        Implements ICalculator
    
    public class CalculatorService : ICalculator
    
  2. Implémentez chaque méthode définie dans l'interface ICalculator dans la classe CalculatorService.

    Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Add
        Dim result As Double = n1 + n2
        ' Code added to write output to the console window.
        Console.WriteLine("Received Add({0},{1})", n1, n2)
        Console.WriteLine("Return: {0}", result)
        Return result
    End Function
    
    Public Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Subtract
        Dim result As Double = n1 - n2
        Console.WriteLine("Received Subtract({0},{1})", n1, n2)
        Console.WriteLine("Return: {0}", result)
        Return result
    
    End Function
    
    Public Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Multiply
        Dim result As Double = n1 * n2
        Console.WriteLine("Received Multiply({0},{1})", n1, n2)
        Console.WriteLine("Return: {0}", result)
        Return result
    
    End Function
    
    
    Public Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Divide
        Dim result As Double = n1 / n2
        Console.WriteLine("Received Divide({0},{1})", n1, n2)
        Console.WriteLine("Return: {0}", result)
        Return result
    
    End Function
    
    public double Add(double n1, double n2)
    {
        double result = n1 + n2;
        Console.WriteLine("Received Add({0},{1})", n1, n2);
        // Code added to write output to the console window.
        Console.WriteLine("Return: {0}", result);
        return result;
    }
    
    public double Subtract(double n1, double n2)
    {
        double result = n1 - n2;
        Console.WriteLine("Received Subtract({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);
        return result;
    }
    
    public double Multiply(double n1, double n2)
    {
        double result = n1 * n2;
        Console.WriteLine("Received Multiply({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);
        return result;
    }
    
    public double Divide(double n1, double n2)
    {
        double result = n1 / n2;
        Console.WriteLine("Received Divide({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);
        return result;
    }
    
    ms734686.note(fr-fr,VS.100).gifRemarque :
    Le code de sortie d'écriture a été ajouté pour faciliter le test.

Exemple

L'exemple de code suivant affiche à la fois l'interface qui définit le contrat et l'implémentation de l'interface.

Imports System
Imports System.ServiceModel


Namespace Microsoft.ServiceModel.Samples
    ' Define a service contract.
    <ServiceContract([Namespace] := "http://Microsoft.ServiceModel.Samples")>  _
    Public Interface ICalculator
        <OperationContract()>  _
        Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double 
        <OperationContract()>  _
        Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double 
        <OperationContract()>  _
        Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double 
        <OperationContract()>  _
        Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double 
    End Interface
    
    ' Step 1: Create service class that implements the service contract.
    Public Class CalculatorService
        Implements ICalculator
        
        ' Step 2: Implement functionality for the service operations.
        Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Add
            Dim result As Double = n1 + n2
            ' Code added to write output to the console window.
            Console.WriteLine("Received Add({0},{1})", n1, n2)
            Console.WriteLine("Return: {0}", result)
            Return result
        End Function

        Public Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Subtract
            Dim result As Double = n1 - n2
            Console.WriteLine("Received Subtract({0},{1})", n1, n2)
            Console.WriteLine("Return: {0}", result)
            Return result
        
        End Function

        Public Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Multiply
            Dim result As Double = n1 * n2
            Console.WriteLine("Received Multiply({0},{1})", n1, n2)
            Console.WriteLine("Return: {0}", result)
            Return result
        
        End Function
        
        
        Public Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Divide
            Dim result As Double = n1 / n2
            Console.WriteLine("Received Divide({0},{1})", n1, n2)
            Console.WriteLine("Return: {0}", result)
            Return result
        
        End Function
    End Class
End Namespace
using System;
using System.ServiceModel;

namespace Microsoft.ServiceModel.Samples
{
    // Define a service contract.
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
    }

    // Step 1: Create service class that implements the service contract.
    public class CalculatorService : ICalculator
    {
         // Step 2: Implement functionality for the service operations.
        public double Add(double n1, double n2)
        {
            double result = n1 + n2;
            Console.WriteLine("Received Add({0},{1})", n1, n2);
            // Code added to write output to the console window.
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Subtract(double n1, double n2)
        {
            double result = n1 - n2;
            Console.WriteLine("Received Subtract({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Multiply(double n1, double n2)
        {
            double result = n1 * n2;
            Console.WriteLine("Received Multiply({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Divide(double n1, double n2)
        {
            double result = n1 / n2;
            Console.WriteLine("Received Divide({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }
    }
}

Le contrat de service est créé et implémenté. Générez la solution pour vous assurer qu'il n'y a pas d'erreurs de compilation, puis poursuivez avec Comment : héberger et exécuter un service Windows Communication Foundation de base pour exécuter le service. Pour obtenir des informations de dépannage, consultez Dépannage du didacticiel de mise en route.

Compilation du code

Si vous utilisez un compilateur de ligne de commande, vous devez référencer l'assembly System.ServiceModel.

Si vous utilisez Visual Studio, dans le menu Générer, cliquez sur Générer la solution (ou appuyez sur Ctrl+Maj+B).

Voir aussi

Tâches

Getting Started, exemple
Self-Host