以下代碼引發了System.ServiceModel.CommunicationException。它調用名爲登錄一個WCF服務操作返回一個EF4 POCO:從WCF服務操作返回EF4 POCO時引發了CommunicationException
var client = new AuthServiceReference.AuthServiceClient();
try
{
Console.Write("Trying to logon...");
var session = client.Login("user", "password"); // throws CommunicationException
Console.WriteLine("done!");
Console.WriteLine("Session ID: {0}. Expires {1}",
session.Id, session.UtcExpires.ToLocalTime());
}
finally
{
client.Close();
}
我一直在調試&尋找幾個小時試圖找出爲什麼這種情況&如何解決它。我已經發現迄今:
- 這可能是一個序列化的問題
- 當我從會議類的所有者成員,異常消失刪除DataMemberAttribute,但這意味着它贏得不會被序列化。
如果有人能夠解釋這個問題,我將不勝感激。
下面是服務合同& POCO類代碼:
[ServiceContract]
public interface IAuthService
{
[OperationContract]
Session Login(string username, string passwordHash);
[OperationContract]
void Logout(Guid sessionId);
}
[DataContract]
public class Session
{
[DataMember]
public Guid Id { get; set; }
[DataMember]
public DateTime UtcCreated { get; set; }
[DataMember]
public DateTime UtcExpires { get; set; }
[DataMember] // serializes correctly if commented out
public virtual User Owner { get; set; }
public static Session Create(User owner)
{
return new Session
{
Owner = owner,
Id = Guid.NewGuid(),
UtcCreated = DateTime.UtcNow,
UtcExpires = DateTime.UtcNow.AddDays(1)
};
}
}
[DataContract]
public class User
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string PasswordHash { get; set; }
[DataMember]
public string PasswordSalt { get; set; }
[DataMember]
public bool IsContributor { get; set; }
[DataMember]
public bool IsConfirmed { get; set; }
[DataMember]
public bool IsAdmin { get; set; }
[DataMember]
public string Email { get; set; }
[DataMember]
public virtual ICollection<Post> Posts { get; set; }
[DataMember]
public virtual ICollection<Comment> Comments { get; set; }
}
用戶類是在我的問題出(會話級以下)。它也用DataContractAttribute裝飾。我會嘗試添加KnownTypeAttribute – 2011-01-06 07:00:30