2017-05-25 14 views
3

私はF#にこのラボでのC#の一部を書き換えています:https://github.com/Microsoft/TechnicalCommunityContent/tree/master/IoT/Azure%20Stream%20Analytics/Session%202%20-%20Hands%20On非同期待つFで#

私は演習6、#17の午前 - SimpleEventProcessorタイプを作成します。
私は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(); 
     } 
    } 

CloseAsync方法

を実装したいと私はこのように始まっ:

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() 
    | _ ->() 

が、私は2つの質問があります:私は返すにはどうすればよい

  1. をF#の世界で待っていますか?
  2. どのように私はNOTの場合 - > C#を返すだけでその可能性を無視します。

答えて

6
  1. 値がAsync<'T>を入力している場合、あなただけの任意のキーワードを指定せずにそれを返すことができます。タイプがTaskまたはTask<'T>の場合、|> Async.AwaitTaskを実行できます。

  2. async { return() }を返すことができます。

だから、あなたはこの取得:

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() } 

別の可能性がasyncワークフロー全体のブロックを入れて、1および2のためのreturnためreturn!を使用することです:

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() 
    } 

事実、非同期ワークフローを使用すると、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

IEventProcessor CloseAsyncはTaskを返します。だから| CloseReason.Shutdown - > context.CheckpointAsync()が機能します。空のタスクを返す方法を理解する必要があります –

+0

これはそれでした:一致理由 | CloseReason.Shutdown - > context.CheckpointAsync() | _ - > Task.CompletedTask –

+0

ああ、申し訳ありませんが、私はあなたの質問を誤って読んで、何とか私はあなたがC#からF#に行くことに加えて、タスクから非同期に変換していると思った。 – Tarmil