c# - 'Service Reference' 和 'Web Reference' 之间的不同行为

标签 c# .net wcf web-services serialization

我按照下面的定义公开了 WCF 端点,

<service name="MyApp.Server.Endpoint.Orange" behaviorConfiguration="MyTio.Server.Endpoint.OrangeBehavior">
  <endpoint address="" binding="basicHttpBinding" contract="Host.Server.Contract.IMyAppApi" bindingNamespace="http://host.com/myapp">
    <identity>
      <dns value="localhost"/>
    </identity>
  </endpoint>
  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>

当我在 .NET 3.5 中添加“服务引用”时,我们在代理中获得了以下类,这非常好:

    [System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="GetMemberBillersRequest", Namespace="http://schemas.datacontract.org/2004/07/Contract.MemberBillers")]
[System.SerializableAttribute()]
public partial class GetMemberBillersRequest : WCFClient.MyRequest {

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private int ApplicationIdField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private int ProductIdField;

    [System.Runtime.Serialization.DataMemberAttribute()]
    public int ApplicationId {
        get {
            return this.ApplicationIdField;
        }
        set {
            if ((this.ApplicationIdField.Equals(value) != true)) {
                this.ApplicationIdField = value;
                this.RaisePropertyChanged("ApplicationId");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public int ProductId {
        get {
            return this.ProductIdField;
        }
        set {
            if ((this.ProductIdField.Equals(value) != true)) {
                this.ProductIdField = value;
                this.RaisePropertyChanged("ProductId");
            }
        }
    }
}

问题是当您在 .NET 2.0 中添加对同一服务的引用时

您将获得同一合约的以下代理:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3082")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://schemas.datacontract.org/2004/07/Contract.MemberBillers")]
public partial class GetMemberBillersRequest : MyRequest {

    private int applicationIdField;

    private bool applicationIdFieldSpecified;

    private int productIdField;

    private bool productIdFieldSpecified;

    /// <remarks/>
    public int ApplicationId {
        get {
            return this.applicationIdField;
        }
        set {
            this.applicationIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool ApplicationIdSpecified {
        get {
            return this.applicationIdFieldSpecified;
        }
        set {
            this.applicationIdFieldSpecified = value;
        }
    }

    /// <remarks/>
    public int ProductId {
        get {
            return this.productIdField;
        }
        set {
            this.productIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool ProductIdSpecified {
        get {
            return this.productIdFieldSpecified;
        }
        set {
            this.productIdFieldSpecified = value;
        }
    }
}

除了通过 .NET 2.0 生成的代理有两个附加字段外,两者完全相同:

productIdFieldSpecified 和 applicationIdFieldSpecified。这两个字段的问题在于,如果您不手动将它们设置为 true,则它们对应的字段(ApplicationId、ProductId)将不会被序列化并传递给服务器!

有人可以向我解释一下这里发生了什么吗?

编辑:

我发现这只发生在 int 类型上,而不是字符串! 这是此操作的数据契约(Contract)

[DataContract]
public class GetMemberBillersRequest : MyRequest
{
    [DataMember]
    public int ApplicationId { get; set; }

    [DataMember]
    public int ProductId { get; set; }
}

最佳答案

这是预期的行为,自 .NET 1.0 以来一直如此。您会看到,对于架构中可选的任何原始类型 - 具有 use="optional"的属性或具有 minOccurs="0"的元素。

如果缺少属性或元素,则无法将生成的属性设置为 null。相反,在这种情况下,*specified 字段设置为 false。在决定“真实”字段是否存在之前,请先检查该字段。

同样,如果要设置main属性,则必须将*specified属性设置为true,否则不会发送。


我相信你知道,但我要为 future 的读者添加这个:是的,现在有可以为 null 的类型。然而,随着 WCF 的出现,ASMX Web 服务的开发速度大大放缓。从未为原始类型实现可空属性并不让我感到惊讶。

另外,请注意:Microsoft: ASMX Web Services are a “Legacy Technology” .

关于c# - 'Service Reference' 和 'Web Reference' 之间的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1128403/

相关文章:

c# - 带有每个字符串标签的 AutoCompleteStringCollection

c# - 如果目录不存在如何创建文件?

asp.net - WCF在IIS下运行?

c# - Kendo MVC ToDataSourceResult 非常慢,IQueryable 很大

c# - 将 System.Diagnostics.ProcessStartInfo StandardOutput 读取为字节而不是字符

c# - List<long> 在 C# 中以逗号分隔的字符串

c# - 以编程方式创建流程图

asp.net - 需要方法的参数值,其中在每个方法中没有特定代码的情况下会引发异常以进行日志记录

c# - 使用 WCF 和自托管的 Windows 身份验证

c# - 简化if条件?