简体   繁体   English

如何调用异步任务类型的方法 <IHttpActionResult> 来自另一个项目?

[英]How can I call a method of type async Task<IHttpActionResult> from another project?

I have a C# Azure application where I have two websites in my solution. 我有一个C# Azure应用程序,在我的解决方案中有两个网站。 One is an outward facing website that a user hits, and the other is an API that the website hits to create users, etc. 一个是用户点击的面向外部的网站,另一个是该网站点击以创建用户的API。

Inside the second project (the API project), I've build a Register method with the code: 在第二个项目(API项目)中,我使用以下代码构建了一个Register方法:

// POST api/Account/Register
[AllowAnonymous]
[Route("Register")]
public async Task<IHttpActionResult> Register(UserModel userModel)
{
     if (!ModelState.IsValid) {
        return BadRequest(ModelState);
     }

     IdentityResult result = await _repo.RegisterUser(userModel);
     IHttpActionResult errorResult = GetErrorResult(result);

     if (errorResult != null) {
         return errorResult;
     }
     return Ok();
}

This works great when calling this through a URL, and it works great when I call it via javascript in the website project. 通过URL调用时效果很好,而通过网站项目中的javascript调用时效果很好。

However, I would like to now call this method from the server side of the website project, yet when I use the code: 但是,我现在想从网站项目的服务器端调用此方法,但是当我使用代码时:

SecondProject.API.Controllers.AccountController UserApiController = new SecondProject.API.Controllers.AccountController();
UserApiController.Register(UserApiModel);

visual studio warns me I should use await . 视觉工作室警告我我应该使用await

So if I add: 因此,如果我添加:

await UserApiController.Register(UserApiModel);

visual studio tells me the await operator can only be used in an async method. visual studio告诉我, await操作符只能在异步方法中使用。

How can I call my Register method inside SecondProject.API from my first project on via server code???? 如何通过服务器代码从我的第一个项目在SecondProject.API调用我的Register方法?

You need to declare the calling method as async . 您需要将调用方法声明为async

Official documentation 官方文件

public async Task CallingMethod()
{
    //...
    SecondProject.API.Controllers.AccountController UserApiController = new SecondProject.API.Controllers.AccountController();
    await UserApiController.Register(UserApiModel);
    //...
}

EDIT: 编辑:

In saying that, you should not be creating controller and calling methods in code. 话虽如此,您不应该在代码中创建控制器和调用方法。 You should be using the repository, as you do in your controller method. 您应该像在控制器方法中一样使用存储库。

public async Task CallingMethod()
{
    //...
    IdentityResult result = await _repo.RegisterUser(userModel);
    //Do something with the result
    //...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何调用异步控制器并返回Task <IHttpActionResult> 直接在C#中 - How to call a controller that is async and return Task<IHttpActionResult> directly in C# 如何调用异步任务<ActionResult<> &gt; 从另一种方法 - How to call an async Task<ActionResult<>> from another method IHttpActionResult vs async Task <IHttpActionResult> - IHttpActionResult vs async Task<IHttpActionResult> 如何测试返回Task的控制器方法 <IHttpActionResult> 匿名类型 - How to Test controller method which returns Task<IHttpActionResult> with anonymous type 如何从另一个 class 调用这种类型的扩展方法 - How can I call this type of extension method from another class 如何在没有停止的情况下在另一个异步方法中创建调用异步方法 - How can I create call an async method inside another async method without it stalling 如何使用Reflection调用类型为“private async Task”的方法 - How to use Reflection to call a method of type “private async Task” 我可以替换一个 await 方法吗? 通过调用异步任务返回? - Can I replace an await method; return with a call to an async Task? 如何从构造函数中调用异步方法? - How can I call async method from constructor? 为什么异步方法返回类型不能从Task派生? - Why async method return type can not be derived from Task?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM