请让我知道如何在 C# 中创建 dapr 自定义中间件 如何注册。 以及如何在微服务中配置它来使用它。
请让我知道详细步骤,因为我在这方面一直很挣扎。
请使用 yaml 配置文件和组件从上到下步骤传递示例应用程序。
甚至不知道如何注册它以及如何在其他微服务中使用它。
创建了这样的中间件
public class DaprCustomAuthMiddleware : IMiddleware
{
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"******************Incoming request: {context.Request.Path}**********************************");
// Call the next middleware in the pipeline
await next(context);
// Log outgoing response
Console.WriteLine($"\"******************Outgoing response: {context.Response.StatusCode}\"******************");
}
}
启动时就是这样的
public class Startup
{
public IConfiguration Configuration { get; }
/// <summary>
/// Configures Services.
/// </summary>
/// <param name="services">Service Collection.</param>
public void ConfigureServices(IServiceCollection services)
{
//services.AddDaprClient();
services.AddTransient<DaprCustomAuthMiddleware>();
services.AddSingleton(new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
});
}
/// <summary>
/// Configures Application Builder and WebHost environment.
/// </summary>
/// <param name="app">Application builder.</param>
/// <param name="env">Webhost environment.</param>
/// <param name="serializerOptions">Options for JSON serialization.</param>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, JsonSerializerOptions serializerOptions,
ILogger<Startup> logger)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseMiddleware<DaprCustomAuthMiddleware>();
app.UseCloudEvents();
app.UseEndpoints(endpoints =>
{
// endpoints.MapSubscribeHandler();
endpoints.MapGet("dapr/subscribe", async context =>
{
// Handle subscription request
// For example: return the subscription response
await context.Response.WriteAsync("{\"topics\": [\"topic1\", \"topic2\"]}");
});
endpoints.MapGet("dapr/config", async context =>
{
// Handle subscription request
// For example: return the subscription response
await context.Response.WriteAsync("Config reached successfully");
});
endpoints.MapGet("/", async context =>
{
// Handle subscription request
// For example: return the subscription response
await context.Response.WriteAsync("Get reached successfully");
});
});
}
}
现在我想使用在 dapr 中注册的这个中间件,以便我可以在微服务通信中使用它。
我尝试过一些事情
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: DaprConsoleApp
spec:
type: middleware.http
version: v1
metadata:
- name: not sure what to enter
value: not sure what to enter code here`
所以我需要从头到尾的步骤的帮助
以下是完成此操作的步骤,包括中间件的 C# 代码、ASP.NET Core 启动配置以及中间件组件的 Dapr YAML 配置。
定义自定义中间件:
public class DaprCustomAuthMiddleware
{
private readonly RequestDelegate _next;
public DaprCustomAuthMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"******************Incoming request: {context.Request.Path}**********************************");
// Call the next middleware in the pipeline
await _next(context);
// Log outgoing response
Console.WriteLine($"******************Outgoing response: {context.Response.StatusCode}******************");
}
启动中的自定义中间件:
public void Configure(IApplicationBuilder app)
{
// Other middleware registrations...
app.UseMiddleware<DaprCustomAuthMiddleware>();
// Other middleware registrations...
}
Dapr 中间件配置:
apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
name: dapr-custom-auth-middleware
spec:
httpPipeline:
handlers:
- name: custom-auth
type: middleware.http.custom
(或)
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: dapr-custom-auth-middleware
spec:
type: middleware.http
version: v1
metadata:
- name: handlerType
value: middleware
输出: