Partager via


SYSLIB0004 : La fonctionnalité de région d’exécution contrainte (CER) n’est pas prise en charge

La fonctionnalité Régions d’exécution contraintes (CER) est prise en charge uniquement dans .NET Framework. Par conséquent, différentes API liées à CER sont marquées comme obsolètes à partir de .NET 5. L’utilisation de ces API génère un avertissement SYSLIB0004 au moment de la compilation.

Les API associées à CER suivantes sont obsolètes :

Toutefois, les API associées à CER suivantes ne sont pas obsolètes :

Solutions de contournement

  • Si vous avez appliqué un attribut CER à une méthode, supprimez l’attribut. Ces attributs n’ont aucun effet dans .NET 5 et versions ultérieures.

    // REMOVE the attribute below.
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    public void DoSomething()
    {
    }
    
    // REMOVE the attribute below.
    [PrePrepareMethod]
    public void DoSomething()
    {
    }
    
  • Si vous appelez RuntimeHelpers.ProbeForSufficientStack ou RuntimeHelpers.PrepareContractedDelegate, supprimez l’appel. Ces appels n’ont aucun effet dans .NET 5 et versions ultérieures.

    public void DoSomething()
    {
        // REMOVE the call below.
        RuntimeHelpers.ProbeForSufficientStack();
    
        // (Remainder of your method logic here.)
    }
    
  • Si vous appelez RuntimeHelpers.PrepareConstrainedRegions, supprimez l’appel. Cet appel n’a aucun effet dans .NET 5 et versions ultérieures.

    public void DoSomething_Old()
    {
        // REMOVE the call below.
        RuntimeHelpers.PrepareConstrainedRegions();
        try
        {
            // try code
        }
        finally
        {
            // cleanup code
        }
    }
    
    public void DoSomething_Corrected()
    {
        // There is no call to PrepareConstrainedRegions. It's a normal try / finally block.
    
        try
        {
            // try code
        }
        finally
        {
            // cleanup code
        }
    }
    
  • Si vous appelez RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup, remplacez l’appel par un bloc try/catch/finally standard.

    // The sample below produces warning SYSLIB0004.
    public void DoSomething_Old()
    {
        RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(MyTryCode, MyCleanupCode, null);
    }
    public void MyTryCode(object state) { /* try code */ }
    public void MyCleanupCode(object state, bool exceptionThrown) { /* cleanup code */ }
    
    // The corrected sample below does not produce warning SYSLIB0004.
    public void DoSomething_Corrected()
    {
        try
        {
            // try code
        }
        catch (Exception ex)
        {
            // exception handling code
        }
        finally
        {
            // cleanup code
        }
    }
    

Supprimer un avertissement

Si vous devez utiliser les API obsolètes, vous pouvez supprimer l’avertissement dans le code ou dans votre fichier projet.

Pour supprimer une seule violation, ajoutez des directives de préprocesseur à votre fichier source pour désactiver, puis réactiver l’avertissement.

// Disable the warning.
#pragma warning disable SYSLIB0004

// Code that uses obsolete API.
// ...

// Re-enable the warning.
#pragma warning restore SYSLIB0004

Pour supprimer tous les avertissements SYSLIB0004 dans votre projet, ajoutez une propriété <NoWarn> à votre fichier projet.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
   ...
   <NoWarn>$(NoWarn);SYSLIB0004</NoWarn>
  </PropertyGroup>
</Project>

Pour plus d’informations, consultez Supprimer des avertissements.

Voir aussi