私は遅れているかもしれないと思うか、C#をあまりにも多く頼んでいますが、これを動作させることはできません。パラメタを使ってジェネリック関数を渡す
私がやろうとしているのは、APIクライアントにいくつかのログ機能をラップし、APIサーバから新しいトークンを要求する方法です。
コントローラ:
public class TestController : ApiController
{
[HttpGet]
[Route("api/model/get/{id}"]
public IHttpActionResult GetModel<Model>(int id)
{
var result = Service.DoHttp<Model>(ServiceClass.GetModel, id);
}
}
サービス:
public static class ServiceClass
{
private static readonly HttpClient client = new HttpClient() { BaseAddress = new Uri(Globals.ExternalApiPath) };
private static string TokenHeader = "";
public async static Task<HttpResponseMessage> GetModel(int id)
{
var response = client.GetAsync($"/api/get/{id}");
return await response;
}
public static T DoHttp<T>(Func<int, HttpResponseMessage> funk, int id)
{
try
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", TokenHeader);
var result = funk(id);
if (result.IsSuccessStatusCode)
{
return result.Content.ReadAsAsync<T>().Result;
}
else
{
throw new Exception(String.Format($"Unknown error! Unable to contact remote API! AccessToken: {TokenHeader} Status code: {result.StatusCode}"));
}
}
catch (Exception e)
{
// log ex
throw e;
}
}
}
しかし、私のService.DoHttp(Service.GetModel、ID);は間違った戻り値型であると不平を言います。
私は間違っていますか、私は全体の概念を誤解しましたか?
EDITは:コンパイラを約'タスクServiceClass.GetModel(int型)' は間違った戻り値の型
「苦情」の正確なテキストを表示できますか? – mjwills
Visual Studiosの苦情が追加されました – HenrikM