我正在发出SOAP请求并得到响应。因此,为了发送请求,我传递了ContractLoad
所映射的ContractLoadDTO
,它工作正常。在读取响应时,我试图将对DTO ContractLoadResult
的响应映射到ContractLoadDTO
,而该映射未映射。
我创建了两个地图:
CreateMap<ContractLoadResult, ContractLoadResultDTO>(); // does not work
CreateMap<ContractLoadDTO, ContractLoad> (); // works
我的课程:
public class ContractLoadDTO
{
public int carrierId { get; set; }
public int contractId { get; set; }
public double amount { get; set; }
public string refNum { get; set; }
}
还有
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3056.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://com.test.carriergroup.service/types")]
public partial class ContractLoad {
private int carrierIdField;
private int contractIdField;
private double amountField;
private string refNumField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public int carrierId {
get {
return this.carrierIdField;
}
set {
this.carrierIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public int contractId {
get {
return this.contractIdField;
}
set {
this.contractIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public double amount {
get {
return this.amountField;
}
set {
this.amountField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
public string refNum {
get {
return this.refNumField;
}
set {
this.refNumField = value;
}
}
}
那些映射正确,没有问题,但是第二个正在工作: 班级:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3056.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://com.test.carriergroup.service/types")]
public partial class ContractLoadResult {
private int carrierIdField;
private int contractIdField;
private double balanceField;
private int errorCodeField;
private string stackTraceField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public int carrierId {
get {
return this.carrierIdField;
}
set {
this.carrierIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public int contractId {
get {
return this.contractIdField;
}
set {
this.contractIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public double balance {
get {
return this.balanceField;
}
set {
this.balanceField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public int errorCode {
get {
return this.errorCodeField;
}
set {
this.errorCodeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
public string stackTrace {
get {
return this.stackTraceField;
}
set {
this.stackTraceField = value;
}
}
}
并且:
public class ContractLoadResultDTO
{
public int carrierIdField { get; set; }
public int contractIdField { get; set; }
public double balanceField { get; set; }
public int errorCodeField { get; set; }
public string stackTraceField { get; set; }
}
将值传递给Soap Requesting时:
ContractLoad[] loads = new ContractLoad[ContractInfo.conractLoads.Length];
loads = Mapper.Map<ContractLoadDTO[], ContractLoad[]>(ContractInfo.conractLoads);
loads
包含ContractInfo.conractLoads
数组中的值。
阅读反应:
ContractLoadResultDTO[] loadResult = new ContractLoadResultDTO[response.loadResults.Length];
loadResult = Mapper.Map<ContractLoadResult[], ContractLoadResultDTO[]>(response.loadResults);
loadResult
中的值为“ 0”。
我做错什么了吗?
答案 0 :(得分:1)
AutoMapper期望一个类中的成员具有与默认情况下从其映射的类中的成员完全相同的名称。在不起作用的示例中,DTO类的每个成员上都添加了field
,因此与其他类不匹配。因此,除非您要手动映射变量,否则DTO类应如下所示:
public class ContractLoadResultDTO
{
public int carrierId { get; set; }
public int contractId { get; set; }
public double balance { get; set; }
public int errorCode { get; set; }
public string stackTrace { get; set; }
}
这只是一条额外的注释,用于向未使用AutoMapper的用户添加上下文,尽管此处存在具有相同名称的私有变量,但AutoMapper无法看到它们,因此无法复制其值。