【问题标题】:How to fix a Circular Reference Issue with WCF如何使用 WCF 修复循环引用问题
【发布时间】:2012-11-19 23:41:46
【问题描述】:

我有一组标准化的表格。所有者,狗,许可证(这些是主要的)每个所有者可以拥有多只狗,每只狗都可以拥有多个许可证。有一些参考表与每个对象相关联,例如所有者的地址、狗的品种和颜色以及许可证的 LicenseType。我不相信这些是我的问题。

这本质上是 Code First(使用逆向工程师 Code First 工具),所以我没有 EDMX。我有一个循环引用问题,这让我很生气。我读过的所有文章都说只是为了把 IsReference = true 属性放在上面。如果只是亲子关系,那就太好了。我有祖父母 - 父母 - 孩子的关系。我试过this 文章但没有运气。我看到了很多文章,但大多数都是 4 岁以上。我想要在.Net 4(不是4.5)中应该做什么和工作我不能成为唯一一个遇到这种情况的人。此刻我的大脑是糊状的。

这是我从数据库中获取对象的代码。它就像一个冠军。

using (DogLicensingContext db = new DogLicensingContext()) {
    Result = (from l in db.Licenses
        .Include(x => x.Dog) // Get the Dog on this License
        .Include(x => x.Dog.Owner)
        .Include(x => x.Dog.Owner.Title)
        .Include(x => x.Dog.Owner.Dogs) // Gets me all of their Dogs
        .Include(x => x.Dog.Owner.Dogs.Select(d => d.Licenses))
        .Include(x => x.Dog.Owner.Dogs.Select(d => d.Licenses.Select(l => l.TagStyle)))
        .Include(x => x.Dog.Owner.Dogs.Select(d => d.Breed))
        .Include(x => x.Dog.Owner.Dogs.Select(d => d.Color))
        .Include(x => x.Dog.Owner.Dogs.Select(d => d.Color1))
        .Include(x => x.Dog.Owner.Dogs.Select(d => d.HairLength))
        .Include(x => x.Dog.Owner.Dogs.Select(d => d.Sex))
        .Include(x => x.Dog.Owner.Address)
        .Include(x => x.Dog.Owner.Address.State)
        .Include(x => x.Dog.Owner.Phones)
        .Include(x => x.Dog.Owner.Emails)
        .Include(x => x.Dog.Owner.Phones.Select(p => p.PhoneType))
        .Include(x => x.Dog.Owner.Emails.Select(p => p.EmailType))
    where l.BarCode == BarCode
    select l).FirstOrDefault();
} // using the database

这是 Owner 和 Dog 类的精简视图。

所有者类:

[DataMember]
public Nullable<System.DateTime> UpdatedOn { get; set; }
[DataMember]
public int ID { get; set; }

public virtual ICollection<Dog> Dogs { get; private set; }

狗类:

[DataMember]
public Nullable<System.DateTime> UpdatedOn { get; set; }
[DataMember]
public int ID { get; set; }

[DataMember]
public virtual Owner Owner { get; set; }

public virtual ICollection<License> Licenses { get; set; }

它像这样在测试客户端中工作。如果我将 ICollection Dogs 或 ICollection Licenses 设置为 DataMember,则当我获得 StackOverflow 时。

那么我怎样才能让集合遇到 WCF?

【问题讨论】:

    标签: wcf entity-framework c#-4.0


    【解决方案1】:

    使用ReferencePreservingDataContractFormathere

    然后用属性标记你的wcf服务方法(操作契约):

    [OperationContract]
    [ReferencePreservingDataContractFormat]
    List<Dog> GetDogsAndOwners();
    

    【讨论】:

    • 感谢您的回复@Salvador Sarpi。我将这些类添加到我的项目中。将 [DataMember] 放在我的两个 ICollection 属性前面并运行它。它获取数据但不能序列化它。 StackOverflow 仍然。如上所示,我还添加了属性。
    • 你不必将ReferencePreservingDataContractFormat属性放在属性上,你需要把它放在WCF操作中(在你的服务的接口中)
    • 我知道,这就是它的样子。 [OperationContract] [ReferencePreservingDataContractFormat] License GetLicenseByBarCode(string BarCode);
    • 你是否关闭了延迟加载和代理创建?
    • 这是我的上下文的样子: public DogLicensingContext() : base("Name=DogLicensingContext") { base.Configuration.ProxyCreationEnabled = false; base.Configuration.LazyLoadingEnabled = false; } - 相同的堆栈溢出
    猜你喜欢
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 2010-12-06
    相关资源
    最近更新 更多