簡體   English   中英

consume ASP.NET Web API functionality on another Web API

[英]consume ASP.NET Web API functionality on another Web API

我在服務器端使用微服務架構 ASP.NET CORE web API。 我有一個數據庫和一個表,但是我在轉換為 EF 以表示數據庫之間的分離時分離了屬性,如下所示:

在此處輸入圖像描述

因此,在一個名為 UserControl 的第一個解決方案(微服務)上,我有:

[Table("User")]
public partial class User
{
    [Key]
    public Guid Id { get; set; }
    [StringLength(255)]
    public string UserName { get; set; } = null!;
    [StringLength(255)]
    public string Password { get; set; } = null!;
    [Column(TypeName = "datetime")]
    public DateTime? LastLogin { get; set; }

}

在另一個名為 ContactsControl 的解決方案(微服務)上,我有:

[Table("User")]
public partial class User
{
    [Key]
    public Guid Id { get; set; }

    [StringLength(255)]
    public string? DisplayName { get; set; }
}

我想創建一個包含兩種解決方案的注冊表單(我的客戶在 Angular 上)——這意味着 UserControl 上的 Post 方法應該添加和更新整個表:

  • 指導 ID
  • 用戶名
  • 密碼
  • 顯示名稱

就像一個真正的注冊表單。 我知道我需要使用 ContactsControl 的功能——我想在添加 UserControl 屬性后,我需要從 ContactsControl 調用我的 UserControl Post 方法(注冊)Update 方法。 我知道我需要使用 HttpClient 但我不確定如何使用它以及在哪里使用它?

用戶控制存儲庫:

 public Guid Add(User user)
    {
        _context.Users.Add(user);
        _context.SaveChanges();
        return user.Id;
        // **call Put method from ContactUser By using HttpClient and Response**

    }

用戶控制 Controller:

    [HttpPost]
    public void Post([FromBody] UserPostDTO user)
    {
        _userRepository.Add(user.ToPostUser());
    }

在保存當前系統之前,您應該將用戶信息發送到另一個系統進行驗證。 假設我們需要比較手機號碼。 如果存在,則返回注冊的提示。

驗證通過后,可以同時保存注冊信息,返回執行結果。 當然也可以將所有注冊的信息返回,然后在當前系統中合並或者保存需要的值。 確保數據一致性。 如果驗證失敗,返回提示即可。


ContactsControl 中的方法

[HttpPost]
public void Post([FromBody] UserPostDTO user)
{
    // Before the current system saves, the data is first forwarded to 
    // UserControl for verification, The verification is passed, 
    // or after saving, the result is returned, assuming true

    var validResult =await _httpClient.GetAsync(" call UserControl method to vaild or save");

    // The judgment condition is just a demonstration, 
    // you can also judge according to the return value

    if(validResult.StatusCode== 201 ){
       // save in current system
       _userRepository.Add(user.ToPostUser());
    }else{
       // save or cancel this operation
       
    }
    
    ...
}

如何在.Net 5中使用`HttpClient`

啟動.cs

public void ConfigureServices(IServiceCollection services)
{
    ...
    // add this line
    services.AddHttpClient();
    ...
    services.AddControllers().AddNewtonsoftJson();
}

Controller

[ApiController]
[Route("api/[controller]")]
public class TestController : ControllerBase
{
    // add this line
    private readonly HttpClient _httpClient;

    private IHostEnvironment _iHostingEnvironment;
    private readonly ILogger<TestController> _logger;

    public TestController(ILogger<TestController> logger, IHostEnvironment iHostingEnvironment, HttpClient httpClient)
    {
        _logger = logger;
        _iHostingEnvironment = iHostingEnvironment;
        _httpClient = httpClient;
    }
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM