【问题标题】:SoapCore Asp.net core 3.1 HeaderSoapCore Asp.net 核心 3.1 标头
【发布时间】:2021-03-02 01:37:36
【问题描述】:

知道如何使用 SoapCore 为我的呼叫添加标头吗?

到目前为止我所拥有的:

在 startup.cs:
app.UseSoapEndpoint<IMyService>("/MyService.svc", new BasicHttpBinding(), SoapSerializer.DataContractSerializer);

在IMyService中

[ServiceContract]
    public interface IMyService
    {      

        [OperationContract]
        public List<SOADataGetService> GetService(string ServiceType, string ServiceName, string ServiceVersion);
        
    }

然后我的肥皂就这样结束了:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:GetService>
         <tem:ServiceType>?</tem:ServiceType>
         <tem:ServiceName>?</tem:ServiceName>
         <tem:ServiceVersion>?</tem:ServiceVersion>
      </tem:GetService>
   </soapenv:Body>
</soapenv:Envelope>

我需要输入&lt;soapenv:Header/&gt; 之类的用户名和密码

【问题讨论】:

    标签: asp.net-core soapcore


    【解决方案1】:

    您可以通过实现和注册自定义IServiceOperationTuner 来访问SoapCore 中的标头,如docs 中所述。

    例如

    public class MyServiceOperationTuner : IServiceOperationTuner
    {
        public void Tune(HttpContext httpContext, object serviceInstance, SoapCore.ServiceModel.OperationDescription operation)
        {
            if (operation.Name.Equals(nameof(MyService.SomeOperationName)))
            {
                MyService service = serviceInstance as MyService;
                service.SetHttpRequest(httpContext.Request);
            }
        }
    }
    
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.TryAddSingleton<IMyService, MyService>();
            services.TryAddSingleton<IServiceOperationTuner>(provider => new MyServiceOperationTuner());
        }
    }
    
    public class MyService : IMyService
    {
        private ThreadLocal<HttpRequest> _httpRequest = new ThreadLocal<HttpRequest>() { Value = null };
    
        public void SetHttpRequest(HttpRequest request)
        {
            _httpRequest.Value = request;
        }
    
        public string SomeOperationName()
        {
            var soapHeader = GetHeaderFromRequest(_httpRequest.Value)
            return $"SOAP Header: {soapHeader}";
        }
    
        private XmlNode GetHeaderFromRequest(HttpRequest request)
        {
            var bytes = (request.Body as MemoryStream)?.ToArray();
            if (bytes == null)
            {
                // Body missing from request
                return null;
            }
    
            var envelope = new XmlDocument();
            envelope.LoadXml(Encoding.UTF8.GetString(bytes));
    
            return envelope.DocumentElement?.ChildNodes.Cast<XmlNode>().FirstOrDefault(n => n.LocalName == "Header");
        }
    }
    

    【讨论】:

      【解决方案2】:

      不要使用过时的SoapCore,尝试使用SmartSoap: https://github.com/Raffa50/SmartSoap

      它也可以作为 nugetPackage 使用: https://www.nuget.org/packages/Aldrigos.SmartSoap.AspNet/

      看一看,试试看,如果您需要进一步的支持,我很乐意为您提供帮助!

      【讨论】:

      • It(SoapCore) 2 个月前发布了新版本,怎么过时了?
      • SoapCore 不支持标头和开发不活跃,因为他们没有在 github 问题跟踪器上回复虽然 SmartSoap 处于活跃状态,但添加了对 .net core 3.1 的支持
      猜你喜欢
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      • 2019-10-03
      相关资源
      最近更新 更多