2017-08-24 6 views
0

私はWCFでいくつかのテストを行いましたが、1つのことを理解することはできません。なぜ、WCF非同期メソッドは同期がFaultExceptionをスローしないのですか?

次のサービスてきた私は、次の実装で

[ServiceContract] 
public interface ICommunicationIssuesService:IService 
{ 
    [OperationContract] 
    void TestExceptionInActionSync(); 
    [OperationContract] 
    Task TestExceptionInActionAsync(); 
} 

を:クライアント側で

public class CommunicationIssuesService : ICommunicationIssuesService 
{ 
    public void TestExceptionInActionSync() 
    { 
     throw new InvalidOperationException(); 
    } 

    public async Task TestExceptionInActionAsync() 
    { 
     throw new InvalidOperationException(); 
    } 
} 

、私はそれで、のChannelFactoryを作成します。

//Test Synchronous 
//... Setup of the channelFactory 
ICommunicationIssuesService channel =_channelFactory.CreateChannel() 
try{ 
    channel.TestExceptionInActionSync(); 
}catch(FaultException<ExceptionDetail>){ 
    //I receive an FaultException 
} 

//Test Asynchronous 
//... Setup of the channelFactory 
ICommunicationIssuesService channel =_channelFactory.CreateChannel() 
try{ 
    channel.TestExceptionInActionAsync(); 
}catch(AggregateException){ 
    //I receive an AggregateException, I guess because it's a Task behind 
} 

私が理解できないことは、ここでFaultException(またはAggregateException)を受け取らない理由です。

答えて

0

この動作はAsync APIsの設計によるもので、これはそのためawait Task非同期実装であるため、あなたは、例外を取得するには、Task.ResultまたはTask.Waitを使用して返されたタスクにアクセスすることもないだろう必要があります。 Wait,Result,awaitのコールは、タスクのステータスにアクセスしようとするときに例外をアンラップするのに役立ちます。これは例外の場合はFaultedで、結果にアクセスしようとします。それは例外を持っている場合は、次のようにTask Status

があなたのコードを変更チェック:

await channel.TestExceptionInActionAsync();

関連する問題