Skip to main content
 首页 » 编程设计

c#之仅当 SoapUI-Request 以特定顺序出现时,WCF 服务才会接收所有字段

2024年02月06日12findumars

这个问题与这个问题非常相似:Not all parameters in WCF data contract make it through the web service call .但是,那里提供的答案并没有解决我的问题。

这是我的服务代码:

using System.Runtime.Serialization; 
using System.ServiceModel; 
namespace SoapUiToWcf 
{ 
    [ServiceContract] 
    public interface IWcfTest 
    { 
        [OperationContract] 
        WcfData ReturnRequest(WcfData wcfData); 
    } 
 
    [DataContract] 
    public class WcfData 
    { 
        [DataMember(Order = 1)] 
        public WcfDataNested NestedData { get; set; } 
 
        public class WcfDataNested 
        { 
            [DataMember(Order = 2)] 
            public string Foo { get; set; } 
            [DataMember(Order = 3)] 
            public string Bar { get; set; } 
        } 
    } 
 
    public class WcfTest : IWcfTest 
    { 
        public WcfData ReturnRequest(WcfData wcfData) 
        { 
            return wcfData; 
        } 
    } 
} 

服务应该返回发送给它的相同数据。当我通过 SoapUI 发送请求时使用下面的数据,一切正常,我得到了所有已发送的值:

<soapenv:Envelope><soapenv:Header/><soapenv:Body><tem:ReturnRequest><tem:wcfData> 
    <soap:NestedData> 
        <soap:Bar>bar</soap:Bar> 
        <soap:Foo>foo</soap:Foo> 
    </soap:NestedData> 
</tem:wcfData></tem:ReturnRequest></soapenv:Body></soapenv:Envelope> 

但是如果我交换请求中的 Bar- 和 Foo- 字段,wcf-service 只接收 foo-value,另一个是 null:

<a:Bar i:nil="true"/> 
<a:Foo>foo</a:Foo> 

编辑

屏幕截图 1(SoapUI 中的请求和响应):

屏幕截图 2(在 Visual Studio 中调试):

我必须如何实现 wcf 服务才能不依赖于请求中字段的排序顺序?

请您参考如下方法:

WCF 希望您按顺序发送它们。如果您不指定顺序,它默认为一些以字母顺序结尾的复杂规则。我认为当您删除订单时,它会首先要求 Bar。它没有的事实可能意味着某些东西在被 SoapUI 调用之前没有刷新。看看另一个线程:https://stackoverflow.com/a/2520005/2540156