2016-08-09 14 views
2

おはよう、これはコミュニティの最初の質問です。私はC#WebAPIフレームワークで作業しています。私はすべてのコントローラのメソッドを非同期に変更しようとしています。私のコントローラは、から拡張されています。GenericControllerこれは、DLLからメソッドを呼び出すメソッド(CallWF)を持ち、すべての種類の例外も処理します。ここでWebAPI: 'IHttpActionResult'に 'GetAwaiter'の定義が含まれていません

Layer1/Controllers <===> Layer2/GenericController.CallWF <===> Layer3/DLL Methods

私GenericController.CallWFコード:

protected IHttpActionResult CallWF<T>(Func<T> action) 
{ 
    try 
    { 
     return Ok(action.Invoke()); 
    } 
    catch (Exception e1) 
    { 
     return(BadRequest(GetMyExceptionMessage(e1))); 
    } 
} 

protected IHttpActionResult CallWF(Action action) 
{ 
    try 
    { 
     action.Invoke(); 
     return Ok(); 
    } 
    catch (Exception e1) 
    { 
     return(BadRequest(GetMyExceptionMessage(e1))); 
    } 
} 

そしてここでは、コントローラのメソッドの例です。

[ResponseType(typeof(string))] 
[Route("MyMethodA")] 
public IHttpActionResult MyMethodA(int arg1) 
{ 
    return CallWF<string>(
    () => { 
     return repositoryA.itsMethodA(arg1); 
    }); 
} 

ご覧のとおり、この方法は同期的です。今私は非同期にしたい。非同期関数を作成する方法を説明するいくつかのWebサイトを読んだ後で、これが解決策であると私は考えていました。

[ResponseType(typeof(string))] 
[Route("MyMethodA")] 
public async Task<IHttpActionResult> MyMethodA(int arg1) 
{ 
    return await CallWF<string>(
    () => { 
     return repositoryA.itsMethodA(arg1); 
    }); 
} 

しかし、これをやって、それが次のエラーが発生します。別のアプローチを試みる

CS1061 'IHttpActionResult' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IHttpActionResult' could be found (are you missing a using directive or an assembly reference?)

、私は非同期のための新しいCallWF関数を作成しようとしました。 ここにアプローチがあります。

protected async Task<IHttpActionResult> CallWFAsync<T>(Func<T> action) 
{ 
    try 
    { 
     IHttpActionResult res = Ok(action.Invoke()); 
     return await Ok(action.Invoke()); 
    } 
    catch (Exception e1) 
    { 
     return (BadRequest(GetMyExceptionMessage(e1))); 
    } 
} 

そして、これをやって、それが私のタイトルのエラーが発生します。

CS1061 'OkNegotiatedContentResult' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'OkNegotiatedContentResult' could be found (are you missing a using directive or an assembly reference?)

これを解決するためのアイデアはありますか?誰かが私を助けることを願っています。

+0

例外メッセージは、あなたがawait-ではない何かを返すようにしようとしていることを説明できる。 'await'は' Task'を返す必要はありません。メソッドに非同期機能が実際に必要な場合を除いて、単に 'Task'を返すことができます。あなたは 'Task.FromResult'を使用することができます – Nkosi

答えて

0

例外メッセージは、あなたが待っていないものを返そうとしていることを説明しています。 awaitは、Taskを返す必要はありません。この方法であなたは、単にこの記事の参照Task.FromResult

使用Taskを返すことができる非同期機能のための実際の必要性がない限り:あなたの最初の試みを使用してhttp://blog.stephencleary.com/2012/02/async-and-await.html

Tip: If you have a very simple asynchronous method, you may be able to write it without using the await keyword (e.g., using Task.FromResult). If you can write it without await, then you should write it without await, and remove the async keyword from the method. A non-async method returning Task.FromResult is more efficient than an async method returning a value.

が、それはこれにリファクタリングすることができます...

[ResponseType(typeof(string))] 
[Route("MyMethodA")] 
public Task<IHttpActionResult> MyMethodA(int arg1) { 
    var result = CallWF<string>(
    () => { 
     return repositoryA.itsMethodA(arg1); 
    }); 

    return Task.FromResult(result); 
} 

CallWFメソッドを変換する場合は、同じ方法を使用できます。

protected Task<IHttpActionResult> CallWFAsync<T>(Func<T> action) { 
    IHttpActionResult result = null; 
    try 
    { 
     result = Ok(action.Invoke()); 
    } catch (Exception e1) { 
     result = BadRequest(GetMyExceptionMessage(e1)); 
    } 
    return Task.FromResult(result); 
} 

そして、これは今、あなたがCallWFAsyncメソッドを呼び出すことによって、あなたの最初の例でやろうとして何をできるようになる

[ResponseType(typeof(string))] 
[Route("MyMethodA")] 
public async Task<IHttpActionResult> MyMethodA(int arg1) 
{ 
    return await CallWFAsync<string>(
    () => { 
     return repositoryA.itsMethodA(arg1); 
    }); 
} 
+0

あなたはすべて働いたと言いました。まず、すべてのリポジトリのメソッドがシンクロ同期であることを知ったので、扱うために慣れたメソッドを作成する必要はありませんでした。それから私はあなたのコードを試して、それは動作します。どうもありがとう。 – SomeName

関連する問題