自主机 WCF SOAP 服务使用私有变量而不是公共变量序列化对象



我有一个非常基本的自承载 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。

有什么方法可以改变这种行为吗?

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

System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults = true(]

最新更新