4

我无法处理一项简单的(如我所想的)任务。我有 2 个 POCO 课程,我想通过 WCF 服务发送:

Estate 类 withc 派生自 Advert 类,而 Locations whitch 与广告相关(广告属于某些位置的多对多关系)

这是我的数据合同:

[DataContract]
[KnownType(typeof(Location))]
[KnownType(typeof(Estate))]
public abstract class Advert
{
    [DataMember]
    public int ID { get; set; }
    [DataMember]
    public decimal? Price { get; set; }
    [DataMember]
    public byte FromAgent { get; set; }
    [DataMember]
    public DateTime Date { get; set; }

    [DataMember]
    public virtual ICollection<Url> Urls { get; set; }
    [DataMember]
    public virtual ICollection<Location> Locations { get; set; }
    [DataMember]
    public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; } 
}

[KnownType(typeof(Location))]
[DataContract]
public class Estate : Advert
{
    [DataMember]
    public byte OfferType { get; set; }

    [DataMember]
    public byte EstateType { get; set; }

    [DataMember]
    public byte MarketType { get; set; }

    [DataMember]
    public int? Area { get; set; }

    [DataMember]
    public byte? Rooms { get; set; }
}


[DataContract]
public class Location
{
    [DataMember]
    public int ID { get; set; }
    [DataMember]
    public byte Type { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string NamePrefix { get; set; }
    [DataMember]
    public string Feature { get; set; }
    [DataMember]
    public int NumberOfEstates { get; set; }

    [DataMember]
    public virtual ICollection<Location> ParentLocations { get; set; }
    [DataMember]
    public virtual ICollection<Advert> Adverts { get; set; }
}

我的服务界面:

    [OperationContract]
    [ServiceKnownType(typeof(Location))]
    IEnumerable<Estate> GetEstates(EstateFilter filter);

它的实现:

    public IEnumerable<Estate> GetEstates(EstateFilter filter)
    {
        return EstateProcesses.GetEstates(filter);
    }

在业务层:

    public static List<Estate> GetEstates(EstateFilter filter)
    {
        List<Estate> estates;

        if (filter==null) filter = new EstateFilter();

        using (var ctx = new Database.Context())
        {
            estates = (from e in ctx.Adverts.OfType<Estate>()
                       where
                              (filter.ID == null || e.ID == filter.ID)
                              && (filter.DateFrom == null || e.Date >= filter.DateFrom)
                              && (filter.DateTo == null || e.Date <= filter.DateTo)
                              && (filter.PriceFrom == null || e.Price >= filter.PriceFrom)
                              && (filter.PriceTo == null || e.Price <= filter.PriceTo)
                              && (filter.FromAgent == null || e.FromAgent == filter.FromAgent)
                              && (filter.LocationID == null || e.Locations.Any(l => l.ID == filter.LocationID))
                              && (filter.PhoneNumber == null || e.PhoneNumbers.Any(p => p.Number == filter.PhoneNumber))
                              && (filter.Url == null || e.Urls.Any(u => u.Address == filter.Url))
                          select e).ToList();
            foreach (var estate in estates)
            {
                ctx.LoadProperty(estate, e => e.Locations);
            }
        }

        return estates;
    }

我获得的例外是波兰语:

Połączenie gniazda zostało przerwane. Mogło to być spowodowane błędnym przetwarzaniem komunikatu, przekroczeniem limitu czasu odbioru przez zdalny host lub problemem z zasobami sieciowymi podległej sieci. Limit czasu lokalnego gniazda wynosi „00:00:59.9929996”.

用英语它会像:

The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was „00:00:59.9929996”.

服务器堆栈跟踪如下:

Server stack trace: 
   w System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, Boolean closing)
   w System.ServiceModel.Channels.SocketConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
   w System.ServiceModel.Channels.DelegatingConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
   w System.ServiceModel.Channels.SessionConnectionReader.Receive(TimeSpan timeout)
   w System.ServiceModel.Channels.SynchronizedMessageSource.Receive(TimeSpan timeout)
   w System.ServiceModel.Channels.FramingDuplexSessionChannel.Receive(TimeSpan timeout)
   w System.ServiceModel.Channels.FramingDuplexSessionChannel.TryReceive(TimeSpan timeout, Message& message)
   w System.ServiceModel.Dispatcher.DuplexChannelBinder.Request(Message message, TimeSpan timeout)
   w System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   w System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   w System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   w System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   w System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   w IService.GetEstates(EstateFilter filter)
   w ServiceClient.GetEstates(EstateFilter filter)

我也将这些类用作 EF 中的 POCO 类。

这就是问题所在:如果我在没有加载位置的情况下发送房地产,它就可以正常工作。但是如果我想发送带有位置的房地产 WCFHostClient 会引发错误。

还有一个问题:如何通过 WCF IEnumerable of Estate 发送每个 Estate 包含 ICollection 的位置?

4

1 回答 1

0

对象树包含很多循环依赖的机会。尝试将此添加到位置

[DataContract(IsReference=true]
public class Location

您可能还必须将其添加到 Advert 类中(广告 -> 位置 -> 广告)。您可能需要考虑对象树以最小化有效负载。

于 2015-05-28T19:32:41.620 回答