多个中间件(REST + SOAP)
Multiple Middlewares (REST + SOAP)
我已经学习了这个教程: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
});
}
我已经学习了这个教程: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
});
}