2017-05-25 2 views
3

Je réécrit une partie du C# dans ce laboratoire à F #: https://github.com/Microsoft/TechnicalCommunityContent/tree/master/IoT/Azure%20Stream%20Analytics/Session%202%20-%20Hands%20OnAsync en F # Attendent

Je suis sur l'exercice 6, # 17 - la création du type SimpleEventProcessor.
Je veux mettre en œuvre la méthode CloseAsync

C#

async Task IEventProcessor.CloseAsync(PartitionContext context, CloseReason reason) 
    { 
     Debug.WriteLine("Processor Shutting Down. Partition '{0}', Reason: '{1}'.", context.Lease.PartitionId, reason); 
     if (reason == CloseReason.Shutdown) 
     { 
      await context.CheckpointAsync(); 
     } 
    } 

et j'ai commencé comme ceci:

member this.CloseAsync(context, reason) = 
    Debug.WriteLine("Processor Shutting Down. Partition '{0}', Reason: '{1}'.", context.Lease.PartitionId, reason) 
    match reason with 
    | CloseReason.Shutdown -> await context.CheckpointAsync() 
    | _ ->() 

mais j'ai 2 questions:

  1. Comment puis-je retourner attendre dans F # monde?
  2. Comment renvoyer le cas NOT -> C# ignore juste cette possibilité.

Répondre

6
  1. Si la valeur est de type Async<'T>, vous pouvez simplement le retourner sans aucun mot-clé. S'il a le type Task ou Task<'T>, vous pouvez faire |> Async.AwaitTask. Vous pouvez renvoyer async { return() }.

Vous obtenez ceci:

member this.CloseAsync(context, reason) = 
    Debug.WriteLine("Processor Shutting Down. Partition '{0}', Reason: '{1}'.", context.Lease.PartitionId, reason) 
    match reason with 
    | CloseReason.Shutdown -> context.CheckpointAsync() |> Async.AwaitTask 
    | _ -> async { return() } 

Une autre possibilité est de mettre tout le bloc dans un flux de travail async et utilisez return! pour 1 et return pour 2:

member this.CloseAsync(context, reason) = 
    async { 
     Debug.WriteLine("Processor Shutting Down. Partition '{0}', Reason: '{1}'.", context.Lease.PartitionId, reason) 
     match reason with 
     | CloseReason.Shutdown -> return! context.CheckpointAsync() |> Async.AwaitTask 
     | _ -> return() 
    } 

En En fait, l'utilisation d'un flux de production asynchrone vous permet de déposer le cas () de la même manière que C#:

member this.CloseAsync(context, reason) = 
    async { 
     Debug.WriteLine("Processor Shutting Down. Partition '{0}', Reason: '{1}'.", context.Lease.PartitionId, reason) 
     if reason = CloseReason.Shutdown then 
      return! context.CheckpointAsync() |> Async.AwaitTask 
    } 
+0

le IEventProcessor CloseAsync renvoie une tâche. Donc | CloseReason.Shutdown -> context.CheckpointAsync() fonctionne. Besoin de savoir comment renvoyer une tâche vide –

+0

L'a fait: match reason avec | CloseReason.Shutdown -> context.CheckpointAsync() | _ -> Task.CompletedTask –

+0

Ah, désolé, j'ai mal lu votre question, d'une certaine manière je pensais que vous étiez en train de passer de la tâche à l'asynchrone en plus de passer de C# à F #. – Tarmil