重写 SoapCore OnWriteBodyContents 方法出现错误

Overriding SoapCore OnWriteBodyContents method gives error

提问人:Bernard Boakye 提问时间:11/17/2023 更新时间:11/17/2023 访问量:9

问:

我正在尝试通过继承 SoapCore 的 CustomMessage 类来自定义我的 SOAP 响应 XML 模式。我已经覆盖了该方法。但是,每当我尝试编写XML的正文内容时,我都会收到以下错误OnWriteBodyContents

System.InvalidOperationException: This message cannot support the operation because it has been written.
   at System.ServiceModel.Channels.Message.EnsureWriteMessageState(XmlDictionaryWriter writer)
   at System.ServiceModel.Channels.Message.WriteBodyContents(XmlDictionaryWriter writer)
   at System.ServiceModel.Channels.Message.WriteBody(XmlDictionaryWriter writer)
   at SoapApi.CustomNamespaceMessage.OnWriteBodyContents(XmlDictionaryWriter writer) in 
   SoapApi\CustomNamespaceMessage.cs:line 25
   at System.ServiceModel.Channels.Message.OnWriteMessage(XmlDictionaryWriter writer)
   at System.ServiceModel.Channels.Message.WriteMessage(XmlDictionaryWriter writer)
   at SoapCore.MessageEncoder.SoapMessageEncoder.WriteMessageAsync(Message message, HttpContext httpContext, PipeWriter pipeWriter)
   at SoapCore.SoapEndpointMiddleware`1.ProcessOperation(HttpContext httpContext, IServiceProvider serviceProvider)
   at SoapCore.SoapEndpointMiddleware`1.Invoke(HttpContext httpContext, IServiceProvider serviceProvider)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

以下是我的代码片段

public class CustomNamespaceMessage : CustomMessage
{
    private const string EnvelopeNamespace = "http://schemas.xmlsoap.org/soap/envelope/";

    public CustomNamespaceMessage() { }

    public CustomNamespaceMessage(Message message) : base(message) { }

    protected override void OnWriteStartEnvelope(XmlDictionaryWriter writer)
    {
        writer.WriteStartElement("env", "Envelope", EnvelopeNamespace);
        writer.WriteAttributeString("xmlns", null, EnvelopeNamespace);
        writer.WriteAttributeString("xmlns", "ns2", null, "http://ws.webgate.bpc.ru/");
    }

    protected override void OnWriteStartBody(XmlDictionaryWriter writer)
    {
        writer.WriteStartElement("env", "Body", EnvelopeNamespace);
    }

    protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
    {
        WriteBodyContennts(writer);        
    }

    public override MessageHeaders Headers => Message.Headers;

    public override MessageProperties Properties => Message.Properties;

    public override MessageVersion Version => Message.Version;
}

在创业公司中,我已经注册了我需要注册的任何内容

public void ConfigureServices(IServiceCollection services)
{
    services.AddSoapCore();
    services.AddAutoMapper(typeof(MappingProfile));

    //Change to Scoped for production
    services.AddScoped<ISwitchedService, SwitchedService>();
    services.AddHttpContextAccessor();

    services.AddMvc().AddXmlSerializerFormatters();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseEndpoints(endpoints =>
    {
        endpoints.UseSoapEndpoint<ISwitchedService, CustomNamespaceMessage>("/SwitchPayment.asmx", new SoapEncoderOptions(), SoapSerializer.XmlSerializer);
        endpoints.MapControllers(); 
    });
}

我做了无数次搜索,但没有一个能满足我的问题。任何帮助将不胜感激。

C# ASP.NET-CORE . NET-CORE 邮递员 SOAPCORE

评论


答: 暂无答案