简体   繁体   English

.NET Core 中的 SoapHttpClientProtocol 等价物

[英]SoapHttpClientProtocol equivalent in .NET Core

I'm trying to invoke a soap web service from .NET Core.我正在尝试从 .NET Core 调用 soap web 服务。

I've built the proxy using do.net-svcutil and found it's a lot different from an older .NET 4.6 implementation of the same endpoint.我使用do.net-svcutil构建了代理,发现它与相同端点的旧 .NET 4.6 实现有很大不同。

The .NET Core proxy doesn't have a class that inherits from System.Web.Services.Protocols.SoapHttpClientProtocol . .NET 核心代理没有继承自 System.Web.Services.Protocols.SoapHttpClientProtocol 的System.Web.Services.Protocols.SoapHttpClientProtocol I understand this namespace is gone in .NET Core, but what has replaced it?我知道这个命名空间在 .NET Core 中消失了,但是什么取代了它?

My advise is to ask for the maker of the service to create a new service that talks swagger. I recently had to consume a soap service, ran into all kinds of specials.我的建议是要求该服务的制造商创建一个与 swagger 通话的新服务。我最近不得不使用 soap 服务,遇到各种特价商品。 Decided to skip the half implemented soap in core.net and called it using a simple post request with a soap envelope in it.决定跳过 core.net 中实现的一半 soap,并使用一个带有 soap 信封的简单发布请求来调用它。 You can use the wsdl to craft your classes you need to serialize to xml. (use paste xml as classes in VS!)您可以使用 wsdl 制作您需要序列化为 xml 的类。(使用粘贴 xml 作为 VS 中的类!)

 private async Task<EnvelopeBody> ExecuteRequest(Envelope request)
    {
        EnvelopeBody body = new EnvelopeBody();
        var httpWebRequest = new HttpRequestMessage(HttpMethod.Post, _serviceUrl);
        string soapMessage = XmlSerializerGeneric<Envelope>.Serialize(request);

        httpWebRequest.Content = new StringContent(soapMessage);

        var httpResponseMessage = await _client.SendAsync(httpWebRequest);
        if (httpResponseMessage.IsSuccessStatusCode)
        {
            using var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync();
            Envelope soapResult;
            var mySerializer = new XmlSerializer(typeof(Envelope));
            using (StreamReader streamReader = new StreamReader(contentStream))
            {
                soapResult = (Envelope) mySerializer.Deserialize(streamReader);
            }
            body = soapResult.Body;
        }
        return body;
    }

My soap envelope looks like this:

   [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
    public partial class Envelope
    {
        private object headerField;

        private EnvelopeBody bodyField;

        /// <remarks/>
        public object Header
        {
            get
            {
                return this.headerField;
            }
            set
            {
                this.headerField = value;
            }
        }

        /// <remarks/>
        public EnvelopeBody Body
        {
            get
            {
                return this.bodyField;
            }
            set
            {
                this.bodyField = value;
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM