Hi there!
I am having a problem related to nhibernate ( at least i think so ... ) .Currently I am working with WCF, so I had to create my own proxies, based on my domain model wich has several classes, for instance - User class wich has a List that point to the latests user accesses and so on . The problem here is that IList (nhibernate collection ) can't be serialized by WCF and so it gaves me an error . What I thought was to create an additional field called accessArray wich is my list but in the array form . I have done that, and initialized the list and the array correctly, the problem is that nhibernate is asking for the virtual keyword on property get/set .
User.cs :
Code:
[DataContract]
public class User : IUser
{
private int id;
[DataMember]
public virtual int Id
{
get { return id; }
set { id = value; }
}
private IList<Access> lstAccess;
[DataMember]
public Access[] accessArray { get; set; }
public virtual IList<Access> LstAccess
{
get { return lstAccess; }
set { lstAccess = value; }
}
private State userState;
private string username;
[DataMember]
public virtual string Username
{
get { return username; }
set { username = value; }
}
private string password;
[DataMember]
public virtual string Password
{
get { return password; }
set { password = value; }
}
private Profile userProfile;
[DataMember]
public virtual Profile UserProfile
{
get { return userProfile; }
set { userProfile = value; }
}
[DataMember]
public virtual State UserState
{
get { return userState; }
set { userState = value; }
}
public User(string username, string password)
{
LstAccess = new List<Access>();
this.username = username;
this.password = password;
this.userState = State.Enabled;
}
public User()
{
LstAccess = new List<Access>();
}
private string EncryptPass(string pwd)
{
var hasher = new MD5CryptoServiceProvider();
byte[] tBytes = Encoding.ASCII.GetBytes(pwd);
byte[] hBytes = hasher.ComputeHash(tBytes);
var sb = new StringBuilder();
for (var c = 0; c < hBytes.Length; c++)
sb.AppendFormat("{0:x2}", hBytes[c]);
return (sb.ToString());
}
public override string ToString()
{
var userData = new StringBuilder();
userData.Append("Id." + this.id);
userData.Append(" Username :" + this.username);
userData.AppendLine("Acessos : ");
foreach (var c in this.lstAccess)
{
userData.AppendLine(c.ToString());
}
return userData.ToString();
}
#region IUser Members
public virtual IUser Login()
{
return UserDAO<User>.Instance().Login(username,EncryptPass(this.password));
}
public virtual bool SaveNewUser()
{
this.password = EncryptPass(this.password);
return GenericDAO<User>.Instance().Add(this);
}
public virtual bool UpdateExistingUser()
{
return GenericDAO<User>.Instance().Update(this);
}
public virtual bool DeleteExistingUser()
{
return GenericDAO<User>.Instance().Remove(this);
}
public virtual bool DeleteAllAccesses()
{
return GenericDAO<Access>.Instance().Remove(this.lstAccess);
}
#endregion
}
[DataContract]
public enum State
{
[EnumMember]
Disabled = 0,
[EnumMember]
Enabled = 1
}
And my
error log :
A first chance exception of type 'NHibernate.InvalidProxyTypeException' occurred in NHibernate.DLL
NHibernate.InvalidProxyTypeException: The following types may not be used as proxies:
Save.Domain.User: method get_accessArray should be 'public/protected virtual' or 'protected internal virtual'
Save.Domain.User: method set_accessArray should be 'public/protected virtual' or 'protected internal virtual'
at NHibernate.Cfg.Configuration.Validate() in d:\My Documents\OSS\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 812
at NHibernate.Cfg.Configuration.BuildSessionFactory() in d:\My Documents\OSS\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 998
at Save.NHibernate.NHibernateHelper.get_SessionFactory() in D:\My Documents\Visual Studio 2008\Projects\Save-Backup\Save\Save\NHibernate\NHibernateHelper.cs:line 22
at Save.NHibernate.NHibernateHelper.OpenSession() in D:\My Documents\Visual Studio 2008\Projects\Save-Backup\Save\Save\NHibernate\NHibernateHelper.cs:line 31
at Save.DAL.UserDAO`1.Login(String username, String password) in D:\My Documents\Visual Studio 2008\Projects\Save-Backup\Save\Save\DAL\UserDAO.cs:line 61
The program '[4288] WebDev.WebServer.EXE: Managed' has exited with code 0 (0x0).
The program '[3860] iexplore.exe: Silverlight' has exited with code 0 (0x0).