在带有实体版本 6 的 WCF 中使用“包含"语法时,复杂对象不会返回 [英] Complex Object won't return when using 'include' syntax in WCF with Entity Version 6
问题描述
好吧,这个让我很难过,因为我刚刚看到我的客户端在返回一个带有包含"的对象时基本上断开连接并终止.但是,它在没有跨 WCF SERVICE 的情况下工作正常.问题是我真的希望通过导航设置那些连接的对象.
Okay so this one has got me stumped as I just see that my client basically disconnects and terminates when returning an object with 'include'. However it works fine without that across a WCF SERVICE. The problem is that I really want those connected objects set up through navigation.
基本上代码是一个复杂的对象返回:
Basically the code is a complex object return:
public teCIF getCif(int aCifId)
{
using (CIFContainer context = GetCifContext())
{
var thing = context.teCIFs
.Include("Product_TYPE")
.FirstOrDefault(i => i.CIF_ID == aCifId);
return thing;
}
}
现在奇怪的是,当我注释掉 include 语句时,这会正常工作.这就像通过 WCF 服务发送的复杂类型不喜欢包含语句,只是说:不,不会那样做".
Now the weird thing is that this will work JUST FINE when I comment out the include statement. It is like the complex type being sent over the WCF service does not like the include statement and just says: "nope, not going to do that".
为了参考我的复杂对象,我已经做了很多修改,因为它是从数据库中生成的 T4 对象(缩写):
For reference my Complex Object I have already hacked at quite a bit as it is a T4 generated object from a database (abbreviated):
[Serializable]
public partial class teCIF
{
public int CIF_ID { get; set; }
public string CUSTOMER_NAME { get; set; }
public string SITE_NAME { get; set; }
[System.Xml.Serialization.XmlIgnore]
public int PRODUCT_TYPE_ID { get; set; }
public virtual tlPRODUCT_TYPE Product_TYPE { get; set; }
}
链接复杂项目的更新:
[Serializable]
public partial class tlPRODUCT_TYPE
{
public tlPRODUCT_TYPE()
{
this.teCIFs = new HashSet<teCIF>();
}
[System.Xml.Serialization.XmlIgnore]
public int PRODUCT_TYPE_ID { get; set; }
public string VALUE { get; set; }
[System.Xml.Serialization.XmlIgnore]
public virtual ICollection<teCIF> teCIFs { get; set; }
}
我已尝试删除属性,但尚未弄乱 WCF 喜欢的数据成员"类型.我只是不明白为什么这不起作用,因为我发誓我在过去的实体版本 4 中使用复杂类型完成了此操作,其中导航属性与其他复杂类型相关.
I have tried removing attributes but have not yet messed with the 'data member' type that WCF likes. I just don't get why this does not work as I have sworn I have done this in the past Entity Version 4 with complex types with navigation properties related to other complex types.
推荐答案
好吧,实际上我从朋友那里得到了帮助,但认为这可能会为其他人提供帮助,所以我应该回答这个问题,因为它可能会帮助其他人.我必须添加一个自定义属性,我为它创建了一个确定循环引用的类.基本上,使用 WCF 时,指向导航的指针会以某种方式丢失,但在未连接到服务时很好.这并不好,因为我希望我的服务使用 WCF 托管方法,而不仅仅是客户端直接使用 Entity V6 调用.
Okay so I actually got help from a friend but figured that this could come up for others so I should answer the question as it may help someone else. I had to add a custom attribute that I make a class for that determines cyclic references. Basically the pointers to the navigation were somehow getting lost when using WCF, but were fine when not hooked up to a service. This is not good as I want my service to be hosting the methods using WCF, not just client calls directly using Entity V6.
[ServiceContract]
public interface ICifService
{
[OperationContract]
[CyclicReferencesAware(true)]
teCIF getCif(int aCifId);
}
实现类的工作方式如下:
The implementation class works like this:
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Runtime.Serialization;
using System.Xml;
using System.Collections.Generic;
[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method)]
public class CyclicReferencesAwareAttribute : Attribute, IContractBehavior, IOperationBehavior
{
private readonly bool _on = true;
public CyclicReferencesAwareAttribute(bool on)
{
_on = on;
}
public bool On
{
get { return _on; }
}
#region IOperationBehavior Members
void IOperationBehavior.AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
void IOperationBehavior.ApplyClientBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.ClientOperation clientOperation)
{
CyclicReferencesAwareContractBehavior.ReplaceDataContractSerializerOperationBehavior(operationDescription, On);
}
void IOperationBehavior.ApplyDispatchBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)
{
CyclicReferencesAwareContractBehavior.ReplaceDataContractSerializerOperationBehavior(operationDescription, On);
}
void IOperationBehavior.Validate(OperationDescription operationDescription)
{
}
#endregion
#region IContractBehavior Members
void IContractBehavior.AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
void IContractBehavior.ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
CyclicReferencesAwareContractBehavior.ReplaceDataContractSerializerOperationBehaviors(contractDescription, On);
}
void IContractBehavior.ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.DispatchRuntime dispatchRuntime)
{
CyclicReferencesAwareContractBehavior.ReplaceDataContractSerializerOperationBehaviors(contractDescription, On);
}
void IContractBehavior.Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
{
}
#endregion
}
public class CyclicReferencesAwareContractBehavior : IContractBehavior
{
private const int MaxItemsInObjectGraph = 2147483647;
private const bool IgnoreExtensionDataObject = false;
private bool _on;
public CyclicReferencesAwareContractBehavior(bool on)
{
_on = on;
}
#region IContractBehavior Members
public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
ReplaceDataContractSerializerOperationBehaviors(contractDescription, _on);
}
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.DispatchRuntime dispatchRuntime)
{
ReplaceDataContractSerializerOperationBehaviors(contractDescription, _on);
}
public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
{
}
internal static void ReplaceDataContractSerializerOperationBehaviors(ContractDescription contractDescription, bool on)
{
foreach (var operation in contractDescription.Operations)
{
ReplaceDataContractSerializerOperationBehavior(operation, on);
}
}
internal static void ReplaceDataContractSerializerOperationBehavior(OperationDescription operation, bool on)
{
if (operation.Behaviors.Remove(typeof(DataContractSerializerOperationBehavior)) || operation.Behaviors.Remove(typeof(ApplyCyclicDataContractSerializerOperationBehavior)))
{
operation.Behaviors.Add(new ApplyCyclicDataContractSerializerOperationBehavior(operation, MaxItemsInObjectGraph, IgnoreExtensionDataObject, on));
}
}
#endregion
}
internal class ApplyCyclicDataContractSerializerOperationBehavior : DataContractSerializerOperationBehavior
{
private readonly int _maxItemsInObjectGraph;
private readonly bool _ignoreExtensionDataObject;
private readonly bool _preserveObjectReferences;
public ApplyCyclicDataContractSerializerOperationBehavior(OperationDescription operationDescription, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences)
: base(operationDescription)
{
_maxItemsInObjectGraph = maxItemsInObjectGraph;
_ignoreExtensionDataObject = ignoreExtensionDataObject;
_preserveObjectReferences = preserveObjectReferences;
}
public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)
{
return new DataContractSerializer(type, name, ns, knownTypes, _maxItemsInObjectGraph, _ignoreExtensionDataObject, _preserveObjectReferences, null /*dataContractSurrogate*/);
}
public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList<Type> knownTypes)
{
return new DataContractSerializer(type, name, ns, knownTypes, _maxItemsInObjectGraph, _ignoreExtensionDataObject, _preserveObjectReferences, null /*dataContractSurrogate*/);
}
}
一旦我将此属性应用于我的服务界面中的任何操作,它就适用于任何复杂类型.
Once I applied this attribute to any Operation in my interface for my service, it worked fine for any complex type.
这篇关于在带有实体版本 6 的 WCF 中使用“包含"语法时,复杂对象不会返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!