Multiple Middlewares (REST + SOAP)

Viewed 446

I've followed this tutorial: custom-asp-net-core-middleware-example

Now I want to add a default REST Middleware, which processes all requests with JSON content but do not find which is the REST Middleware from ASP.NET when I do not register my own middleware.

Can someone tell me how to use multiple middlewares where one is a SOAP and the other is a REST middleware?

Here is my code to register the middleware:

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);
    }
}

The SOAPEndpointMiddleware.cs is mostly the same as in the tutorial.

1 Answers

To add 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
    });
}
Related