You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 7中如何读取SOAP消息体中的Header内容?

在.NET 7中使用SoapCore读取SOAP Header内容

方案一:通过SoapHeaderAttribute绑定自定义Header类

这是SoapCore官方推荐的原生方案,直接将SOAP Header映射为.NET实体类:

  1. 定义匹配SOAP Header的实体类
    严格对应AuthSoapHeader的结构与命名空间:

    using System.Xml.Serialization;
    
    [XmlRoot("AuthSoapHeader", Namespace = "https://someurl.ru/")]
    public class AuthSoapHeader
    {
        public string Login { get; set; }
        public string Password { get; set; }
    }
    
  2. 在SOAP服务中注入并读取Header
    在服务接口和实现类中,用SoapHeaderAttribute标记要接收的Header:

    using SoapCore;
    
    public interface IMySoapService
    {
        [SoapHeader("AuthHeader")]
        void MethodName();
    }
    
    public class MySoapService : IMySoapService
    {
        // 自动绑定SOAP Header到该属性
        public AuthSoapHeader AuthHeader { get; set; }
    
        public void MethodName()
        {
            // 直接获取Header内容
            string login = AuthHeader.Login;
            string password = AuthHeader.Password;
            // 后续业务逻辑处理
        }
    }
    
  3. 配置SoapCore服务
    在Program.cs中完成服务注册与端点配置:

    var builder = WebApplication.CreateBuilder(args);
    builder.Services.AddSoapCore();
    builder.Services.AddSingleton<IMySoapService, MySoapService>();
    
    var app = builder.Build();
    app.UseSoapEndpoint<IMySoapService>("/Service.asmx", new SoapEncoderOptions());
    app.Run();
    

方案二:全局拦截读取Header(ISoapMessageInspector)

如果需要统一处理所有SOAP请求的Header,可实现消息检查器:

  1. 实现自定义检查器

    using SoapCore;
    using System.ServiceModel.Channels;
    
    public class AuthHeaderInspector : ISoapMessageInspector
    {
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            // 从请求中提取指定Header
            var authHeader = request.Headers.GetHeader<AuthSoapHeader>("AuthSoapHeader", "https://someurl.ru/");
            if (authHeader != null)
            {
                string login = authHeader.Login;
                string password = authHeader.Password;
                // 可将信息存入HttpContext或直接做权限验证
            }
            return null;
        }
    
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            // 发送响应前的操作(可选)
        }
    }
    
  2. 注册检查器到Soap端点

    app.UseSoapEndpoint<IMySoapService>("/Service.asmx", new SoapEncoderOptions(), 
        inspectors: new List<ISoapMessageInspector> { new AuthHeaderInspector() });
    

关于IHttpContextAccessor读取请求体为空的说明

SoapCore会先读取并解析请求体,默认情况下请求体流是不可重读的。若一定要通过HttpContext读取,需先启用请求体缓冲:

// 在Program.cs中添加配置
builder.Services.Configure<Microsoft.AspNetCore.Http.Features.FormOptions>(options =>
{
    options.BufferBody = true;
    options.BufferBodyLengthLimit = 1024 * 1024; // 根据实际请求大小调整
});

但这种方式需要手动解析XML,效率远低于前两种方案,不推荐使用。

内容的提问来源于stack exchange,提问作者Ruslan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 22:10:10