[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class NHEmployeeService
{
[OperationContract]
public IList<PersonalInfo> GetAllEmployees()
{
return Global.CurrentSession.CreateCriteria(typeof(PersonalInfo)).List<PersonalInfo>();
}
}
WCF+NHibernate
Сервис возвращает список сотрудников. Каждый сотрудник имеет номер, имя, фамилию, дату рождения и семейное положение (тянется из словаря).
Код службы:
Код:
Вызов службы на клиенте:
Код:
var proxy = new NHServiceReference.NHEmployeeServiceClient();
proxy.GetAllEmployeesCompleted += new System.EventHandler<NHServiceReference.GetAllEmployeesCompletedEventArgs>(proxy_GetAllEmployeesCompleted);
proxy.GetAllEmployeesAsync();
proxy.GetAllEmployeesCompleted += new System.EventHandler<NHServiceReference.GetAllEmployeesCompletedEventArgs>(proxy_GetAllEmployeesCompleted);
proxy.GetAllEmployeesAsync();
Классы NHibernate объектов:
Код:
[DataContract]
public class PersonalInfo
{
[DataMember]
public virtual int EmployeeID { get; protected set; }
[DataMember]
public virtual string FirstName { get; set; }
[DataMember]
public virtual string LastName { get; set; }
[DataMember]
public virtual DateTime BirthDate { get; set; }
[DataMember]
public virtual FamilyStatus FamilyStatus { get; set; }
}
[DataContract]
public class FamilyStatus
{
[DataMember]
public virtual int StatusID { get; set; }
[DataMember]
public virtual string StatusName { get; set; }
[DataMember]
public virtual IList<PersonalInfo> Employees { get; set; }
}
public class PersonalInfo
{
[DataMember]
public virtual int EmployeeID { get; protected set; }
[DataMember]
public virtual string FirstName { get; set; }
[DataMember]
public virtual string LastName { get; set; }
[DataMember]
public virtual DateTime BirthDate { get; set; }
[DataMember]
public virtual FamilyStatus FamilyStatus { get; set; }
}
[DataContract]
public class FamilyStatus
{
[DataMember]
public virtual int StatusID { get; set; }
[DataMember]
public virtual string StatusName { get; set; }
[DataMember]
public virtual IList<PersonalInfo> Employees { get; set; }
}
Мапинги:
Код:
public class PersonalInfoMap : ClassMap<PersonalInfo>
{
public PersonalInfoMap()
{
Table("PersonalInfo");
Id(x => x.EmployeeID).GeneratedBy.Identity();
Map(x => x.FirstName).Column("FirstName");
Map(x => x.LastName).Column("LastName");
Map(x => x.BirthDate).Column("BirthDate");
References(x => x.FamilyStatus, "FamilyStatus");
}
}
public class FamilyStatusMap : ClassMap<FamilyStatus>
{
public FamilyStatusMap()
{
Table("FamilyStatus");
Id(x => x.StatusID).GeneratedBy.Identity();
Map(x => x.StatusName).Column("StatusName");
HasMany(x => x.Employees).AsList().Inverse().Cascade.None();
}
}
{
public PersonalInfoMap()
{
Table("PersonalInfo");
Id(x => x.EmployeeID).GeneratedBy.Identity();
Map(x => x.FirstName).Column("FirstName");
Map(x => x.LastName).Column("LastName");
Map(x => x.BirthDate).Column("BirthDate");
References(x => x.FamilyStatus, "FamilyStatus");
}
}
public class FamilyStatusMap : ClassMap<FamilyStatus>
{
public FamilyStatusMap()
{
Table("FamilyStatus");
Id(x => x.StatusID).GeneratedBy.Identity();
Map(x => x.StatusName).Column("StatusName");
HasMany(x => x.Employees).AsList().Inverse().Cascade.None();
}
}
При выполнеии вылетает сообщение об ошибке:
"Type 'Castle.Proxies.FamilyStatusProxy' with data contract name 'FamilyStatusProxy:http://schemas.datacontract.org/2004/07/Castle.Proxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer."
Добавление атрибута KnownTypeAttribute не помогло:
Код:
[KnownTypeAttribute(typeof(FamilyStatus))]
[DataContract]
public class PersonalInfo
{
[DataMember]
public virtual int EmployeeID { get; protected set; }
[DataMember]
public virtual string FirstName { get; set; }
[DataMember]
public virtual string LastName { get; set; }
[DataMember]
public virtual DateTime BirthDate { get; set; }
[DataMember]
public virtual FamilyStatus FamilyStatus { get; set; }
}
[DataContract]
public class PersonalInfo
{
[DataMember]
public virtual int EmployeeID { get; protected set; }
[DataMember]
public virtual string FirstName { get; set; }
[DataMember]
public virtual string LastName { get; set; }
[DataMember]
public virtual DateTime BirthDate { get; set; }
[DataMember]
public virtual FamilyStatus FamilyStatus { get; set; }
}
Подскажите, пожалуйста, как исправить ошибку?