【问题标题】:Self Host WCF SOAP Service is serialising objects using the private variables rather than the public ones自托管 WCF SOAP 服务使用私有变量而不是公共变量来序列化对象
【发布时间】:2018-04-16 14:13:32
【问题描述】:

我有一个非常基本的自托管 WCF 控制台应用程序,它托管一个 SOAP 服务。不幸的是,当我查看对服务中方法调用的响应时,序列化的响应实际上是序列化的私有变量而不是公共变量。进行调用的客户端并不期望这种情况,因此在响应中看不到任何有效值。服务代码如下:

using (ServiceHost host = new ServiceHost(typeof(MyService), baseAddress))
{
    // Enable metadata publishing.
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
    host.Description.Behaviors.Add(smb);

    host.Open();

    ... ...
}

public class MyService: IMyService
{
    public MyReturnType MyMethod()
    {
        return new MyReturnType(); 
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.2612.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")][System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.aaa123.com/WebServices")]
 public partial class MyReturnType {

    private string myPropertyField;

    public string MyProperty { get { return myPropertyField; } set { this.myPropertyField = value; } }

所以在上面对 MyMethod 的调用中,我会看到响应 XML 中的属性定义为 myPropertyField,而不是 MyProperty。

有没有办法改变这种行为?

【问题讨论】:

    标签: c# soap datacontractserializer


    【解决方案1】:

    如果我在接口中的方法声明上添加下面的属性,似乎可以正确序列化响应:

    System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults = true)]

    【讨论】:

      猜你喜欢
      • 2011-05-25
      • 2013-01-02
      • 2012-06-03
      • 2012-01-25
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      相关资源
      最近更新 更多