Call async Task<> from controller in ASP.NET MV

2019-06-04 11:40发布

I have a Library wrapper OVH API and i try to call a function for get my consumer key in a ASP.NET MVC project. It works in a Console project application but the method never response in my Controller : the method in the Library

public async Task<CredentialsResponse> RequestCredential(IEnumerable<AccessRule> accessRules, string redirectUrl = null)
    {
        Ensure.NotNull("accessRules", accessRules);

        CredentialsRequest cmd = new CredentialsRequest();
        cmd.AccessRules.AddRange(accessRules);
        cmd.Redirection = redirectUrl;

        if (cmd.AccessRules.Count == 0)
            throw new ArgumentException("You must specify at least one accessRule");
        return await RawCall<CredentialsResponse>(HttpMethod.Post, "/auth/credential", cmd;
    }

and i call in the controller :

public ActionResult Index()
    {
        Information infosClient = new Information();

        OvhApiClient api = new OvhApiClient("", "", OvhInfra.Europe);

        CredentialsResponse response = api.RequestCredential(new[]{
            new AccessRule{ Method = "GET", Path = "/*"},
            new AccessRule{ Method = "PUT", Path = "/*"},
            new AccessRule{ Method = "POST", Path = "/*"},
            //new AccessRule{ Method = "DELETE", Path = "/*"},
        }).Result;

        api.ConsumerKey = response.ConsumerKey;

        infosClient.ConsumerKey = api.ConsumerKey;
        return View(infosClient);
    }

I already tried quite a lot of things without success (put the call in a method async for example).

Thanks in advance for your help

1条回答
相关推荐>>
2楼-- · 2019-06-04 12:38

Make the controller action async:

public async Task<ActionResult> Index()
{
    ...
    CredentialsResponse response = await api.RequestCredential(...);
    ...
}
查看更多
登录 后发表回答