多个中间件(REST+SOAP)

c#

我遵循了本教程:custom-asp-net-core-middleware-example

现在我想添加一个默认的 REST 中间件,它处理所有带有 JSON 内容的请求,但当我不注册自己的中间件时,找不到来自 ASP.NET 的 REST 中间件。

有人能告诉我如何使用多个中间件,其中一个是 SOAP,另一个是 REST 中间件吗?

这是我注册中间件的代码:

public static class SOAPEndpointExtensions
{
    public static IApplicationBuilder UseSOAPEndpoint(this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<SOAPEndpointMiddleware>();
    }

    public static IApplicationBuilder UseSOAPEndpoint<T>(this IApplicationBuilder builder, string path, MessageEncoder encoder)
    {
        return builder.UseMiddleware<SOAPEndpointMiddleware>(typeof(T), path, encoder);
    }

    public static IApplicationBuilder UseSOAPEndpoint<T>(this IApplicationBuilder builder, string path, Binding binding)
    {
        var encoder = binding.CreateBindingElements().Find<MessageEncodingBindingElement>()?.CreateMessageEncoderFactory().Encoder;
        return builder.UseMiddleware<SOAPEndpointMiddleware>(typeof(T), path, encoder);
    }
}

SOAPEndpointMiddleware.cs与教程中的大致相同。

回答

添加 REST + SOAP:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers(); // REST API

    //  .... 
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseSOAPEndpoint<YourService>("/YourService.svc", new BasicHttpBinding());

    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers(); // REST API
    });
}


以上是多个中间件(REST+SOAP)的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>