gpt4 book ai didi

c# - 从 WCF 服务操作返回 EF4 POCO 时引发 CommunicationException

转载 作者:行者123 更新时间:2023-11-30 22:42:05 25 4
gpt4 key购买 nike

以下代码引发 System.ServiceModel.CommunicationException。它正在调用一个名为 Login 的 WCF 服务操作,该操作返回一个 EF4 POCO:

        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();
}

我一直在调试和搜索数小时,试图找出为什么会发生这种情况以及如何解决它。到目前为止我发现了什么:

  1. 这可能是一个序列化问题
  2. 当我从 Session 类的 Owner 成员中删除 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; }
}

最佳答案

事实证明,这是使用 WCF 序列化 POCO 代理时的一个已知问题。有一个 MSDN walkthough这解释了如何使用 System.Data.Objects.ProxyDataContractResolver 解决它。

本质上,您创建一个名为 ApplyDataContractResolverAttribute 的新类,并将其应用于返回 POCOS 的服务方法:

[ServiceContract]
public interface IAuthService
{
[OperationContract]
[ApplyDataContractResolver]
Session Login(string username, string passwordHash);
}

using System;
using System.Data.Objects;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace WcfExampleBlog.Services
{
public class ApplyDataContractResolverAttribute : Attribute, IOperationBehavior
{
#region IOperationBehavior Members

public void AddBindingParameters(OperationDescription description, BindingParameterCollection parameters)
{
}

public void ApplyClientBehavior(OperationDescription description, ClientOperation proxy)
{
var dataContractSerializerOperationBehavior =
description.Behaviors.Find<DataContractSerializerOperationBehavior>();
dataContractSerializerOperationBehavior.DataContractResolver =
new ProxyDataContractResolver();
}

public void ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch)
{
var dataContractSerializerOperationBehavior =
description.Behaviors.Find<DataContractSerializerOperationBehavior>();
dataContractSerializerOperationBehavior.DataContractResolver =
new ProxyDataContractResolver();
}

public void Validate(OperationDescription description)
{
// Do validation.
}

#endregion
}
}

关于c# - 从 WCF 服务操作返回 EF4 POCO 时引发 CommunicationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4605910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com