尝试使用 net core / net 5 连接 SOAP 服务

Posted

技术标签:

【中文标题】尝试使用 net core / net 5 连接 SOAP 服务【英文标题】:trying to connection SOAP service using net core / net 5 【发布时间】:2021-06-16 20:31:33 【问题描述】:

我正在使用从市场返回订单列表的 SOAP 服务。

问题是当我从 SOAP 服务请求订单时,结果总是为空。尝试了很多东西,但仍然没有改变。

ConnectedService.json


  "ProviderId": "Microsoft.VisualStudio.ConnectedService.Wcf",
  "Version": "15.0.40203.910",
  "ExtendedData": 
    "inputs": [
      "https://api.n11.com/ws/OrderService.wsdl"
    ],
    "collectionTypes": [
      "System.Collections.Generic.Dictionary`2",
      "System.Collections.Generic.List`1"
    ],
    "namespaceMappings": [
      "*, OrderService"
    ],
    "sync": true,
    "targetFramework": "net5.0",
    "typeReuseMode": "None"
  

我的用户 SOAP 服务的 c# 代码

var rere = await orderService.DetailedOrderListAsync(new DetailedOrderListRequest
    
        auth = authentication,
        pagingData = new PagingData
        
            pageSize = 100,
            currentPage = 0,
        ,
        searchData = new OrderDataListRequest
                                
            period = new OrderSearchPeriod
            
                startDate = "01/03/2021",
                endDate = "19/03/2021"
            ,
        
    );

请求 XML 是

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <DetailedOrderListRequest xmlns="http://www.n11.com/ws/schemas">
            <auth xmlns="">
                <appKey>XXXXXXXXXXXXXXXXX</appKey>
                <appSecret>XXXXXXXXXX</appSecret>
            </auth>
            <searchData xmlns="">
                <productId xsi:nil="true"/>
                <status xsi:nil="true"/>
                <productSellerCode xsi:nil="true"/>
                <sameDayDelivery xsi:nil="true"/>
                <period>
                    <startDate>01/03/2021</startDate>
                    <endDate>19/03/2021</endDate>
                </period>
                <sortForUpdateDate>false</sortForUpdateDate>
            </searchData>
            <pagingData xmlns="">
                <currentPage>0</currentPage>
                <pageSize>100</pageSize>
                <totalCount xsi:nil="true"/>
                <pageCount xsi:nil="true"/>
            </pagingData>
        </DetailedOrderListRequest>
    </s:Body>
</s:Envelope>

我从 FIDDLER Web 调试器中捕获了它,并且来自 SOAP 服务的响应也使用 fiddler 捕获了

响应 XML:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <ns3:DetailedOrderListResponse xmlns:ns3="http://www.n11.com/ws/schemas">
            <result>
                <status>success</status>
            </result>
            <pagingData>
                <currentPage>0</currentPage>
                <pageSize>100</pageSize>
                <totalCount>57</totalCount>
                <pageCount>1</pageCount>
            </pagingData>
            <orderList>
                <order>
                    <citizenshipId>TTTTTTT</citizenshipId>
                    <createDate>01/03/2021 17:05</createDate>
                    <id>TTTTTTTT</id>
                    <orderItemList>
                        <orderItem>
                            <approvedDate>02/03/2021</approvedDate>
                            <attributes/>
                            <commission>14.99</commission>
                            <customTextOptionValues/>
                            <deliveryFeeType>3</deliveryFeeType>
                            <dueAmount>142.41</dueAmount>
                            <id>TTTTTT</id>
                            <installmentChargeWithVAT>0</installmentChargeWithVAT>
                            <mallDiscount>7.49</mallDiscount>
                            <price>154.4</price>
                            <productId>TTTT</productId>
                            <productName>TTTTTTTT</productName>
                            <productSellerCode>211131</productSellerCode>
                            <quantity>1</quantity>
                            <sellerCouponDiscount>0</sellerCouponDiscount>
                            <sellerDiscount>4.5</sellerDiscount>
                            <sellerInvoiceAmount>149.9</sellerInvoiceAmount>
                            <shipmenCompanyCampaignNumber>TTTTT</shipmenCompanyCampaignNumber>
                            <shipmentInfo>
                                <campaignNumber>TTT</campaignNumber>
                                <campaignNumberStatus>1</campaignNumberStatus>
                                <shipmentCode>258739698</shipmentCode>
                                <shipmentCompany>
                                    <id>344</id>
                                    <name>Yurtiçi</name>
                                    <shortName>YK</shortName>
                                </shipmentCompany>
                                <shipmentMethod>1</shipmentMethod>
                                <trackingNumber>TTTTT</trackingNumber>
                            </shipmentInfo>
                            <shippingDate>02/03/2021</shippingDate>
                            <sppApproved>true</sppApproved>
                            <status>10</status>
                            <totalMallDiscountPrice>7.49</totalMallDiscountPrice>
                            <updatedDate>06/03/2021 19:00</updatedDate>
                            <version>502</version>
                        </orderItem>
                    </orderItemList>
                    <orderNumber>TTTTT</orderNumber>
                    <paymentType>1</paymentType>
                    <status>5</status>
                    <totalAmount>149.9</totalAmount>
                    <totalDiscountAmount>11.99</totalDiscountAmount>
                </order>
            </orderList>
        </ns3:DetailedOrderListResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

如您在上面看到的,我面临的问题我已成功发送请求,SOAP 服务将结果发送给我,但在 net core 或 net 5 上无法反序列化上面也添加的结果

响应结果截图

我尝试了很多不同的方法,但无法解决返回 null 的问题。 需要一些帮助..

WSDL 服务网址:https://api.n11.com/ws/OrderService.wsdl

编辑: net 5自动生成的WSDL服务引用类的一部分

    /// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.n11.com/ws/schemas")]
public partial class DetailedOrderListResponse


    private ResultInfo resultField;

    private DetailedOrderData[] orderListField;

    private PagingData pagingDataField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
    public ResultInfo result
    
        get
        
            return this.resultField;
        
        set
        
            this.resultField = value;
        
    

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 1)]
    [System.Xml.Serialization.XmlArrayItemAttribute("order", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
    public DetailedOrderData[] orderList
    
        get
        
            return this.orderListField;
        
        set
        
            this.orderListField = value;
        
    

【问题讨论】:

【参考方案1】:

我不知道它在 .NET 5 中的样子,但在 .net core 3.1 中,当我使用 WCF 服务时,我必须添加一些代码才能使其正常工作

public interface IBindingProvider

    Binding Get();


public class BindingProvider : IBindingProvider

    public Binding Get()
                
        var transportBinding = new HttpTransportBindingElement();
        var textEncodingBinding = new TextMessageEncodingBindingElement(MessageVersion.Soap11, System.Text.Encoding.UTF8);

        return new CustomBinding(transportBinding, textEncodingBinding);
    

绑定类当然是从

using System.ServiceModel.Channels;

在我的 Startup.cs 中我不得不使用

 app.UseEndpoints(endpoints =>
 
      var binding = app.ApplicationServices.GetRequiredService<IBindingProvider>().Get();
      endpoints.MapControllers();
      endpoints.UseSoapEndpoint<ISomeService>(Constants.SoapHttpRequestPath, binding, SoapSerializer.XmlSerializer);
 );

SoapSerializer 来自

using SoapCore;

【讨论】:

我在同一个项目中使用了至少 5-6 个 WCF 服务,并且不需要额外的代码。但是这个真的很烦人。 你能显示你的类 OrderList 和 OrderListField 添加了自动生成的参考文件部分。还有 WSDL 服务 URL,所以也许你可以生成完整的 WSDL 引用 我花了将近 2 天的时间终于得到了解决方案。只需从参考文件中删除 Order 属性。添加了答案【参考方案2】:

这完全是浪费时间将近 2 天

第一张图片有 Order 属性,第二张没有。

因此,如果您是 net core/net 5+ 开发人员,那么这是一个救命技巧。从 n11 个市场订单服务中删除订单属性。这样就可以得到订单列表了。

【讨论】:

以上是关于尝试使用 net core / net 5 连接 SOAP 服务的主要内容,如果未能解决你的问题,请参考以下文章

在 .NET Core/.NET 5+ 中使用 TransactionScope 和 SQL 连接池时如何避免 PlatformNotSupportedException

.NET 5 在使用 EF Core Power Tools 时正确注入 DbContext 连接字符串

尝试使用 .Net Core 3.1 连接 mySAP WCF 时出现问题

在 .net core 6 中配置连接字符串

Httpclient在.net core3.1上成功运行,但更新到.net 5中连接超时(System.Net.Sockets.SocketException (10060)) C#

.net core 5 worker 服务的无密码连接字符串