简体   繁体   English

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

[英]How do you configure Brighter with ASP.NET Core 7?

How do you configure Brighter with ASP.NET Core 7?如何使用 ASP.NET Core 7 配置 Brighter? Brighter documentation is pretty sparse on details, sample on the inte.net seems outdated too. Brighter 文档在细节上相当稀疏,inte.net 上的示例似乎也已过时。

Is there sample startup code (Program.cs) and a basic Controller available on the web? web 上是否有示例启动代码 (Program.cs) 和基本的 Controller?

I got it to work, here is a sample code.我得到它的工作,这里是一个示例代码。

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.

相关问题 在 ASP.NET 内核中,你在哪里配置重定向登录? - In ASP.NET Core, where do you configure redirect to login? 如何在 ASP.NET Core 的 OData 中导航/配置从复杂类型到实体类型的导航? - How do you navigate/configure navigation from complex type to entity type in OData in ASP.NET Core? 如何配置 Kestrel 以使用随机动态端口并在运行时使用 ASP.NET Core 3.1 确定端口? - How do you configure Kestrel to use a random dynamic port and determine the port at run-time with ASP.NET Core 3.1? 如何在 ASP.NET Core 6 中为 IConfiguration 实现 DI? - How do you implement DI for IConfiguration in ASP.NET Core 6? 如何链接两个模型ASP.NET CORE - How do you link two models ASP.NET CORE 在 ASP.NET Core 中,如何检查请求是否是本地的? - In ASP.NET Core how do you check if request is local? 如何在 ASP.Net Core MVC 中测试 NotFound() - How do you test for NotFound() in ASP.Net Core MVC 你如何在 ASP.NET Core 中强制使用小写路由? - How do you enforce lowercase routing in ASP.NET Core? 如何在 ASP.NET Core 中创建自定义 AuthorizeAttribute? - How do you create a custom AuthorizeAttribute in ASP.NET Core? 如何将AWS ASP.NET Core日志记录添加到ASP.NET Core 2.0 Razor Pages应用程序? - How do you add AWS ASP.NET Core Logging to an ASP.NET Core 2.0 Razor Pages app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM