C# 使用自动属性标记类型将生成的字段作为WCF客户端代理代码上的成员
我有一个DTO类型声明如下:C# 使用自动属性标记类型将生成的字段作为WCF客户端代理代码上的成员,c#,wcf,C#,Wcf,我有一个DTO类型声明如下: [Serializable] public class PersonDTO { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } [OperationContract] public PersonDTO GetPerson(int id); <xs:schema elemen
[Serializable]
public class PersonDTO
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
[OperationContract]
public PersonDTO GetPerson(int id);
<xs:schema elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/Test.DTO">
−
<xs:complexType name="PersonDTO">
−
<xs:sequence>
<xs:element name="_x003C_Id_x003E_k__BackingField" nillable="true" type="xs:int"/>
<xs:element name="_x003C_FirstName_x003E_k__BackingField" nillable="true" type="xs:string"/>
<xs:element name="_x003C_LastName_x003E_k__BackingField" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="IVQueueDTO" nillable="true" type="tns:IVQueueDTO"/>
</xs:schema>
我有一项WCF服务,其运营合同如下:
[Serializable]
public class PersonDTO
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
[OperationContract]
public PersonDTO GetPerson(int id);
<xs:schema elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/Test.DTO">
−
<xs:complexType name="PersonDTO">
−
<xs:sequence>
<xs:element name="_x003C_Id_x003E_k__BackingField" nillable="true" type="xs:int"/>
<xs:element name="_x003C_FirstName_x003E_k__BackingField" nillable="true" type="xs:string"/>
<xs:element name="_x003C_LastName_x003E_k__BackingField" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="IVQueueDTO" nillable="true" type="tns:IVQueueDTO"/>
</xs:schema>
我遇到的问题是,当我使用“添加服务引用”使用此服务时,wsdl包含以下内容:
[Serializable]
public class PersonDTO
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
[OperationContract]
public PersonDTO GetPerson(int id);
<xs:schema elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/Test.DTO">
−
<xs:complexType name="PersonDTO">
−
<xs:sequence>
<xs:element name="_x003C_Id_x003E_k__BackingField" nillable="true" type="xs:int"/>
<xs:element name="_x003C_FirstName_x003E_k__BackingField" nillable="true" type="xs:string"/>
<xs:element name="_x003C_LastName_x003E_k__BackingField" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="IVQueueDTO" nillable="true" type="tns:IVQueueDTO"/>
</xs:schema>
−
−
当我尝试在WCF客户机上引用时,我得到的不是person.Id和person.FirstName,而是person.Idk\u BackingField、person.FirstNamek\u BackingField等
我应该怎么做才能得到我在WCF服务端定义的确切类型?我在PersonDTO上使用Serializable属性,因为该服务需要与java互操作。我正在使用.NETFramework4.0、C#、VisualStudio2010、WinXPSP3。WCF服务公开http端点并使用basicHttpBinding。您是否尝试过使用DataContact和DataMember属性而不是Serializable?我相信这仍然会给您一个Java可以访问的可序列化对象。DataContact解决了这个问题。但我不确定DataContract是否可以与Java互操作。我还没有测试这个。但是我很确定上面描述的问题是由于Serializable属性造成的。查看此链接。这使我相信DataContract应该是可互操作的
是的,关键是DataContract和DataMember属性允许您指定名称和其他元数据。请参阅@matrix,如果此解决方案有效,请将答案标记为已接受(向上/向下投票下的复选框)。非常感谢。