循环引用和WCFWCF

2023-09-03 16:51:03 作者:不顾

我已经使用POCO发生器产生的我POCO实体,我有超过150个以上的表在我的数据库。 我分享POCO实体都在应用程序层,包括客户端。我在我的context.I残疾人既LazyLoading和ProxyCreation正在使用WCF在我的​​数据访问和业务层之上。

I have generated my POCO entities using POCO Generator, I have more than 150+ tables in my database. I am sharing POCO entities all across the application layers including the client. I have disabled both LazyLoading and ProxyCreation in my context.I am using WCF on top of my data access and business layer.

现在,当我返回POCO实体我的客户,我得到一个错误说的基础连接已关闭的我的WCF跟踪,发现确切的错误: 包含周期,如果引用跟踪被禁用不能序列的。

Now, When I return a poco entity to my client, I get an error saying "Underlying connection was closed" I enabled WCF tracing and found the exact error : Contains cycles and cannot be serialized if reference tracking is disabled.

我看了一下MSDN,发现解决方案,如设置IsReference =在DataContract方法atttribute真实的,但我不是装饰我的POCO类与DataContracts,我认为没有必要的它。我不会被调用,作为一个POCO,如果我装点一类具有DataContract属性

I Looked at MSDN and found solutions like setting IsReference=true in the DataContract method atttribute but I am not decorating my POCO classes with DataContracts and I assume there is no need of it as well. I won't be calling that as a POCO if I decorate a class with DataContract attribute

然后,我发现了解决方案,如采用自定义属性[CyclicReferenceAware]在我ServiceContracts.That没有工作,但我想抛出这个问题的社区,看看别人是如何管理这也是为什么微软没有给内置支持搞清楚循环引用,而序列化POCO类

Then, I found solutions like applying custom attribute [CyclicReferenceAware] over my ServiceContracts.That did work but I wanted to throw this question to community to see how other people managed this and also why Microsoft didn't give built in support for figuring out cyclic references while serializing POCO classes

推荐答案

您已经提到过这种方法,但我用这个属性。

You already mentioned the approach, but I use this attribute

public class ReferencePreservingDataContractFormatAttribute : Attribute, IOperationBehavior
    {
        #region IOperationBehavior Members
        public void AddBindingParameters(OperationDescription description, BindingParameterCollection parameters)
        {
        }

        public void ApplyClientBehavior(OperationDescription description, System.ServiceModel.Dispatcher.ClientOperation proxy)
        {
            IOperationBehavior innerBehavior = new ReferencePreservingDataContractSerializerOperationBehavior(description);
            innerBehavior.ApplyClientBehavior(description, proxy);
        }

        public void ApplyDispatchBehavior(OperationDescription description, System.ServiceModel.Dispatcher.DispatchOperation dispatch)
        {
            IOperationBehavior innerBehavior = new ReferencePreservingDataContractSerializerOperationBehavior(description);
            innerBehavior.ApplyDispatchBehavior(description, dispatch);
        }


        public void Validate(OperationDescription description)
        {
        }
    #endregion
}

} ......以及对服务,像这样的操作参考;

} ...and reference on an operation on the Service like so;

[OperationContract]
[ReferencePreservingDataContractFormat]
IList<SomeObject> Search(string searchString);

仅供参考 - 想给​​信贷,这是由于,但没有记录,我原来看到上面的方法

FYI - would like to give credit where it's due, but did not record where I originally saw the above approach.

编辑:

我相信的code源来自该博客文章。

I believe source of the code is from this blog post.