如何使用.NET Core 6接受soap请求

问题描述 投票:0回答:2

我需要将旧的肥皂服务转换为.NET Core 6,以便它可以与我们的其他服务驻留在同一服务器上。

我想接受肥皂请求,内容类型

application/soap+xml
,然后我的代码将处理该请求。然后它会返回一个相同格式的响应,
application/soap+xml

我查遍了互联网,但无法完全让它发挥作用。这是我到目前为止所拥有的,我收到一个错误:

System.InvalidOperationException:XML 文档中存在错误 (1, 172)。

System.InvalidOperationException:不是预期的。

要求:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                 xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
    <soap12:Body>
        <CoverageRequest xmlns="http://www.iicmva.com/CoverageVerification/">
            <RequestorInformation>
            </RequestorInformation>
            <Detail>
            </Detail>
        </CoverageRequest>
    </soap12:Body>
</soap12:Envelope>

我的

program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddScoped<IProcess, ProcessRepo>();
builder.Services.AddScoped<ISoapHelper, SoapHelper>();
builder.Services.AddScoped<IData, VerifyDataRepo>();
builder.Services.AddControllers().AddXmlSerializerFormatters();

var config = builder.Configuration;

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.Run();

我的控制器端点:

[ApiController]
public class DMVController : ControllerBase
{
    private readonly IProcess _process;

    public DMVController(IProcess process)
    {
        _process = process;
    }

    [Consumes("application/soap+xml")]
    [Produces("application/soap+xml")]
    [HttpPost, Route("/VerifyInsurance")]
    public IActionResult Post([FromBody] string value)
    {
        var returnValue = _process.ProcessRequest(value);
    }
}
soap asp.net-core-6.0
2个回答
1
投票

目前我正在转换一个使用 WCF/Soap 的旧 VB.NET 应用程序。 CoreWCF 库似乎对我有用。

https://github.com/CoreWCF/CoreWCF

我建议您首先尝试做一些最小的事情,演练对此非常有用,看看它是否适合您的需求。

https://github.com/CoreWCF/CoreWCF/blob/main/Documentation/Walkthrough.md

请注意,如果要使用 TLS 1.2+,调用客户端必须使用 .NET Framework 4.6.2 或更高版本。

PS SoapUI 客户端也可以像这个库一样工作。


0
投票

我找到了一种方法来完成我需要的事情,而无需向该项目添加更多依赖项。我创建了一个自定义输入格式。我发现一篇文章对我有帮助这里

首先,在program.cs 文件中添加自定义格式化程序。 SoapInputFormatter 是我对 TextInputFormatter 类的自定义扩展:

builder.Services.AddControllers(options =>
{
    options.InputFormatters.Insert(0, new SoapInputFormatter());
});

在 SoapInputFormatter 类中,您重写 CanReadType 和 ReadRequestBodyAsync:

using System.Text;
using Microsoft.Net.Http.Headers;
using Microsoft.AspNetCore.Mvc.Formatters;

public class SoapInputFormatter : TextInputFormatter
{
    public SoapInputFormatter() 
    {
        SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/soap+xml"));
        SupportedEncodings.Add(Encoding.UTF8);
        SupportedEncodings.Add(Encoding.Unicode);
    }

    protected override bool CanReadType(Type type)
       => type == typeof(string);

    public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
    {
        var httpContext = context.HttpContext;
        using var reader = new StreamReader(httpContext.Request.Body, encoding);
        try
        {
            var line = await reader.ReadToEndAsync();
            return await InputFormatterResult.SuccessAsync(line);
        }
        catch (Exception)
        {
            return await InputFormatterResult.FailureAsync();
        }
    }
}

正如您在屏幕截图中看到的,我现在有了我的请求,我可以处理它。

© www.soinside.com 2019 - 2024. All rights reserved.