当服务结构无状态服务返回POCO对象图时发生COMException

本文关键字:服务 COMException 对象图 返回 结构 状态 POCO | 更新日期: 2023-09-27 18:01:34

我有一个无状态服务,它加载并返回一个实体对象数组(使用EF的POCO)。延迟加载和代理创建被禁用。

一切都工作得很好,只要我只返回一个级别图:

var devices = context.Devices.Where(d => d.ParentHost_Id == hostId);
return Task.FromResult(devices.ToArray());

但是,如果我想包含另一个级别,事情就会变得很糟糕:

var devices = context.Devices.Where(d => d.ParentHost_Id == hostId).Include(d => d.ConnectedDevices);
return Task.FromResult(devices.ToArray());

在这种情况下,我的代码将加载并返回请求的对象,没有任何麻烦,但是在调用链的上游某处Service Fabric抛出一个COMException,然后它通过再次调用我的服务来处理。这会导致一个新的comexception,它会一直这样做,直到我停止它。

{System.Runtime.InteropServices.COMException (0x80071C4C): Undantag från HRESULT: 0x80071C4C
vid Microsoft.ServiceFabric.Services.Communication.FabricTransport.Common.NativeServiceCommunication.IFabricServiceCommunicationClient.EndRequest(IFabricAsyncOperationContext context)
vid Microsoft.ServiceFabric.Services.Communication.FabricTransport.Client.NativeServiceCommunicationClient.EndRequest(IFabricAsyncOperationContext context)}

Devices类由EF生成,看起来像这样:

public partial class Devices
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Devices()
    {
        this.LogValues = new HashSet<LogValues>();
    }
    public long Id { get; set; }
    public int DeviceId { get; set; }
    public int Type { get; set; }
    public string Property { get; set; }
    public string Name { get; set; }
    public Nullable<long> ParentHost_Id { get; set; }
    public virtual Hosts Hosts { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<LogValues> LogValues { get; set; }
    public virtual ConnectedDevices ConnectedDevices { get; set; }
}

任何关于为什么会发生这种情况的想法将是感激的!

当服务结构无状态服务返回POCO对象图时发生COMException

显然,问题是我的对象图包含一个循环引用(ConnectedDevice有一个对Device的引用)。我从EF模型中删除了它,现在一切都按预期工作。