[英]How do you configure Brighter with ASP.NET Core 7?
如何使用 ASP.NET Core 7 配置 Brighter? Brighter 文档在细节上相当稀疏,inte.net 上的示例似乎也已过时。
web 上是否有示例启动代码 (Program.cs) 和基本的 Controller?
我得到它的工作,这里是一个示例代码。
using Paramore.Brighter;
using Paramore.Brighter.Extensions.DependencyInjection;
using Polly;
using Polly.Registry;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services
.AddBrighter(options =>
{
var retryPolicy = Policy.Handle<Exception>().WaitAndRetry(new[]
{
TimeSpan.FromMilliseconds(50),
TimeSpan.FromMilliseconds(100),
TimeSpan.FromMilliseconds(200)
});
var circuitBreakerPolicy = Policy.Handle<Exception>().CircuitBreaker(2,
TimeSpan.FromMilliseconds(500));
var retryPolicyAsync = Policy.Handle<Exception>()
.WaitAndRetryAsync(new[]
{ TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(200) });
var circuitBreakerPolicyAsync =
Policy.Handle<Exception>().CircuitBreakerAsync(2, TimeSpan.FromMilliseconds(500));
options.PolicyRegistry = new PolicyRegistry
{
{ CommandProcessor.RETRYPOLICY, retryPolicy },
{ CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy },
{ CommandProcessor.RETRYPOLICYASYNC, retryPolicyAsync },
{ CommandProcessor.CIRCUITBREAKERASYNC, circuitBreakerPolicyAsync }
};
options.HandlerLifetime = ServiceLifetime.Scoped;
options.CommandProcessorLifetime = ServiceLifetime.Scoped;
options.MapperLifetime = ServiceLifetime.Singleton;
})
.AutoFromAssemblies(typeof(MyService).Assembly);
builder.Services.AddScoped<MyService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
public partial class Program
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.