Skip to content

Latest commit

 

History

History
53 lines (40 loc) · 1019 Bytes

File metadata and controls

53 lines (40 loc) · 1019 Bytes

中 | EN

MinimalAPI

原始用法:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/api/v1/helloworld", ()=>"Hello World");
app.Run();

用例:

Install-Package MASA.Contrib.Service.MinimalAPIs
  1. 添加MinimalAPI
var builder = WebApplication.CreateBuilder(args);
var app = builder.Services
                 .AddServices(builder);
  1. 自定义Service并继承ServiceBase,如:
public class IntegrationEventService : ServiceBase
{
    public IntegrationEventService(IServiceCollection services) : base(services)
    {
        App.MapGet("/api/v1/payment/HelloWorld", HelloWorld);
    }

    public string HelloWorld()
    {
        return "Hello World";
    }
}

提示:继承ServiceBase的服务为单例模式注册,如果需要从DI获取获取

public async Task DeleteBasketByIdAsync(string id, [FromServices] IBasketRepository repository)
{
    await repository.DeleteBasketAsync(id);
}