未能调用该服务时,该方法返回列表< T> [英] Failed to invoke the service when the method returns List<T>
问题描述
我是新来的WCF,我有一个简单的问题。
I am new to wcf and I have a simple question.
我用EF与WCF服务。 WCF服务从WinForms应用程序调用。我想从WCF获得简单的列表。当我从调用WCF测试控制台AllVisitors()我得到下一个错误:
I use EF with WCF service. WCF service called from WinForms app. I want get simple List from WCF. When i invoke AllVisitors() from wcf test console i get next error:
无法调用服务。可能的原因:服务是脱机
或无法访问;客户端配置不匹配,
代理;现有的代理是无效的。请参考$ B $堆栈轨迹B的更多细节。您可以尝试通过启动一个新的代理来恢复,恢复
默认配置,或刷新服务。
Failed to invoke the service. Possible causes: The service is offline or inaccessible; the client-side configuration does not match the proxy; the existing proxy is invalid. Refer to the stack trace for more detail. You can try to recover by starting a new proxy, restoring to default configuration, or refreshing the service.
在跟踪登录我看下exaption:
In trace log i see next exaption:
有尝试序列参数
的 http://tempuri.org/:AllVisitorsResult 。该消息的InnerException是
'类型
'System.Data.Entity.DynamicProxies.Visitor_4E7A6F28B7631289C58E0883F6BDF681C78B7180EA3E530CDFE420CAA3C15645'
数据合同名称
Visitor_4E7A6F28B7631289C58E0883F6BDF681C78B7180EA3E530CDFE420CAA3C15645:的 http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies '$ b预计不会$ b的。考虑使用DataContractResolver或添加不是静态已知的已知类型的列表中的任何
型 - 通过使用KnownTypeAttribute属性或将它们添加到已知类型传递给了
名单为例,
。DataContractSerializer的
There was an error while trying to serialize parameter http://tempuri.org/:AllVisitorsResult. The InnerException message was 'Type 'System.Data.Entity.DynamicProxies.Visitor_4E7A6F28B7631289C58E0883F6BDF681C78B7180EA3E530CDFE420CAA3C15645' with data contract name Visitor_4E7A6F28B7631289C58E0883F6BDF681C78B7180EA3E530CDFE420CAA3C15645:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' 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.
下面是我的代码:
[ServiceContract]
public interface IServiceVisit
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedResponse)]
List<Visitor> AllVisitors();
}
[DataContract]
[KnownType(typeof(Visitor))]
[KnownType(typeof(Visit))]
[KnownType(typeof(ICollection<Visit>))]
public class ServiceVisit : IServiceVisit
{
public List<Visitor> AllVisitors()
{
using (TurnstileDbEntities te = new TurnstileDbEntities())
{
return te.Visitors.ToList<Visitor>();
}
}
}
当我尝试的方法失败返回列表与LT;访问者>
。但是,当我返回原始类型的方法工作完全正常。 。如int,十进制数,字符串等...
The method fails when I try return a List<Visitor>
. But the method works perfectly fine when I return a primitive type. Such as int, decimal, string...etc.
下面访客和访问自动生成的模型:
Here Visitor and Visit auto generated model:
public partial class Visitor
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Visitor()
{
this.Visits = new HashSet<Visit>();
}
public int Id { get; set; }
public int PermitId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public bool IsValid { get; set; }
public System.DateTime RegistrationDate { get; set; }
public byte[] Picture { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Visit> Visits { get; set; }
}
public partial class Visit
{
public int Id { get; set; }
public int VisitType { get; set; }
public System.DateTime VisitDate { get; set; }
public Nullable<int> Visitor_Id { get; set; }
public virtual Visitor Visitor { get; set; }
}
我知道列表<>
填充数据。而且我知道,因为字节[]图片包含二进制数据(图像)数据的大小可大了。
也与参观者参观为一对多。
我了解的问题是序列化和生成的动态代理与名称类似:Visitor_4E7A6F28B7631289C58E0883F6BDF681C78B7180EA3E530CDFE420CAA3C15645。但是,也许我错了。
所以,我怎么能解决这个问题,我到底做错了什么?我发现这个问题disscused regulary。但不能为我找到解决办法。
I know that List<>
is populated with data. And I know that size of data can be big because byte[] Picture contains binary data (images).
Also Visitor related to Visit as one to many.
As I understood problem is in serialization and in generated dynamic proxies with names like: Visitor_4E7A6F28B7631289C58E0883F6BDF681C78B7180EA3E530CDFE420CAA3C15645. But maybe I'm wrong.
So, how can I fix this issue and what am I doing wrong? I found that this issue disscused regulary. But can't find solution for me.
推荐答案
所以,对于这个isssue解决方案是设置ProxyCreationEnabled = FALSE
So, solution for this isssue is to set ProxyCreationEnabled = false
public List<Visitor> AllVisitors()
{
using (TurnstileDbEntities te = new TurnstileDbEntities())
{
te.Configuration.ProxyCreationEnabled = false;
return te.Visitors.ToList<Visitor>();
}
}
这篇关于未能调用该服务时,该方法返回列表< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!