Protobuf-net WCF响应为空

我有一个WCF合同,概述了一种测试方法,该方法仅使用protobuf-net在WCF中返回一个类的实例。我可以在测试应用程序中进行序列化和反序列化,但是当我通过WCF发出请求时,响应存在该类实例,但是其所有属性均为null。 以下是相关的配置文件和类定义:
[ProtoContract]
public class TestClass
{
    [ProtoMember(1)]
    public int TestInt { get; set; }

    [ProtoMember(2)]
    public string TestString { get; set; }
}

...

[ServiceContract]
public interface ITestService
{
    [OperationContract]
    [ProtoBehavior]
    TestClass RunTest(int x);
}

...

<extensions>
    <behaviorExtensions>
        <add name=\"protobuf\" type=\"ProtoBuf.ServiceModel.ProtoBehaviorExtension, protobuf-net, Version=1.0.0.282, Culture=neutral, PublicKeyToken=257b51d87d2e4d67\" />
    </behaviorExtensions>
</extensions>

<endpointBehaviors>
    <behavior name=\"Proto.Default.EndpointBehavior\">
        <protobuf />
    </behavior>
</endpointBehaviors>
<serviceBehaviors>
    <behavior name=\"Proto.Default.ServiceBehavior\">
      <serviceDebug includeExceptionDetailInFaults=\"true\" />
      <serviceMetadata />
      <serviceAuthorization principalPermissionMode=\"None\" />
      <serviceThrottling    maxConcurrentCalls=\"250\"
                            maxConcurrentSessions=\"200\"
                            maxConcurrentInstances=\"10\" />
    </behavior>
</serviceBehaviors>

...

<services>
    <service name=\"WcfTestService\" behaviorConfiguration=\"Proto.Default.ServiceBehavior\">
    <endpoint address=\"\"    binding=\"netTcpBinding\" bindingConfiguration=\"NetTcpBinding_ITestService\"   contract=\"ITestService\" behaviorConfiguration=\"Proto.Default.EndpointBehavior\" />
    <endpoint address=\"mex\" binding=\"customBinding\" bindingConfiguration=\"myMexTcpBinding\" contract=\"IMetadataExchange\" />
    <host>
        <baseAddresses>
            <add baseAddress=\"net.tcp://localhost:2517/TestService\" />
        </baseAddresses>
    </host>
</service>

...

<client>
   <endpoint address=\"net.tcp://localhost:2517/TestService\"
    binding=\"netTcpBinding\" bindingConfiguration=\"NetTcpBinding_ITestService\"
    contract=\"ITestService\" name=\"TestService\"
    behaviorConfiguration=\"Proto.Default.EndpointBehavior\">
    <identity>
        <dns value=\"localhost\" />
    </identity>
</endpoint>
</client>
我可以调试服务,然后看到请求。将创建并返回TestClass对象。我逐步浏览了protobuf-net源代码,并运行了deserialize方法,该方法仅创建了TestClass的空白实例,并遍历了返回的数据,但从未设置任何属性。 哦,我用Mex生成代理。 编辑 这是MEX为TestClass生成的类
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute(\"System.Runtime.Serialization\", \"4.0.0.0\")]
[System.Runtime.Serialization.DataContractAttribute(Name=\"TestClass\", Namespace=\"http://schemas.datacontract.org/2004/07/TestProject\")]
[System.SerializableAttribute()]
public partial class TestClass : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

    [System.NonSerializedAttribute()]
    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

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

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string TestStringField;

    [global::System.ComponentModel.BrowsableAttribute(false)]
    public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
        get {
            return this.extensionDataField;
        }
        set {
            this.extensionDataField = value;
        }
    }

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

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string TestString {
        get {
            return this.TestStringField;
        }
        set {
            if ((object.ReferenceEquals(this.TestStringField, value) != true)) {
                this.TestStringField = value;
                this.RaisePropertyChanged(\"TestString\");
            }
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}
    
已邀请:
对;墨西哥/一代未包括这些数字,但您可以添加它们。在单独的代码文件(相同的名称空间)中,添加
[ProtoContract]
[ProtoPartialMember(1, \"TestInt\")]
[ProtoPartialMember(2, \"TestString\")]
partial class TestClass {}
有时WCF会有所帮助,有时会很痛苦(请注意,如果两端都有共享的DTO组件,则工作起来很容易)。 有时WCF会在每个DataMember上给出正确的数字,但以1为准-如果发生这种情况,您可以使用一个微调来告诉protobuf使用带有偏移量的那些数字。     
我认为您在这里遇到了著名的互操作性问题。请参阅我对这篇文章的回答: 在客户端,WCF操作合同的返回值为null!有什么办法吗? 您需要标识客户端代码期望的Soap消息,并更正与服务返回的Soap消息同步的代码。 在此示例中,您可以通过以下方式确定客户希望响应消息格式化的方式,
a. creating Service stub from WSDL using WSDL /serverInterface
b. Create a class implimenting the Service stub
c. Host the WebService in IIS and browse the help page
d. Click on operation called from list.
e. Help page shows the expected request and response soap message
    

要回复问题请先登录注册