简体   繁体   English

consume ASP.NET Web API functionality on another Web API

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

I'm using microservices architecture on my server side with ASP.NET CORE web API.我在服务器端使用微服务架构 ASP.NET CORE web API。 I have one database and one table but I'm separating the properties while converting to EF to represent the separation between the database, like this:我有一个数据库和一个表,但是我在转换为 EF 以表示数据库之间的分离时分离了属性,如下所示:

在此处输入图像描述

So that on one first solution (microservice)- called UserControl I have:因此,在一个名为 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; }

}

And on the other solution (microservice)- called ContactsControl I have:在另一个名为 ContactsControl 的解决方案(微服务)上,我有:

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

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

I want to create a registration form (my client is on Angular) that includes both of the solutions – meaning that Post method on UserControl should add and update the entire table:我想创建一个包含两种解决方案的注册表单(我的客户在 Angular 上)——这意味着 UserControl 上的 Post 方法应该添加和更新整个表:

  • Guid Id指导 ID
  • UserName用户名
  • Password密码
  • DisplayName显示名称

Like a real Registration Form.就像一个真正的注册表单。 I understand that I need to consume the functionality from ContactsControl – I think I need to call on my UserControl Post Method (registration) the Update method from ContactsControl after I'm adding the UserControl properties.我知道我需要使用 ContactsControl 的功能——我想在添加 UserControl 属性后,我需要从 ContactsControl 调用我的 UserControl Post 方法(注册)Update 方法。 I know I need to use HttpClient but I'm not sure how to use it and where?我知道我需要使用 HttpClient 但我不确定如何使用它以及在哪里使用它?

UserControl repository:用户控制存储库:

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

    }

UserControl Controller:用户控制 Controller:

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

Before saving the current system, you should send the user information to another system for verification.在保存当前系统之前,您应该将用户信息发送到另一个系统进行验证。 Suppose we need to compare the mobile phone number.假设我们需要比较手机号码。 If it exists, return the registered prompt.如果存在,则返回注册的提示。

When the verification is passed, the registration information can be saved at the same time, and the execution result can be returned.验证通过后,可以同时保存注册信息,返回执行结果。 Of course, all the registered information can be returned, and then merged in the current system or save the required value.当然也可以将所有注册的信息返回,然后在当前系统中合并或者保存需要的值。 Ensure data consistency.确保数据一致性。 If the verification fails, just return to the prompt.如果验证失败,返回提示即可。


Method in ContactsControl 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
       
    }
    
    ...
}

How to use `HttpClient` in.Net 5如何在.Net 5中使用`HttpClient`

Startup.cs启动.cs

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

Controller 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