簡體   English   中英

如何使用 ASP.NET Core 7 配置 Brighter?

[英]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.

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