gpt4 book ai didi

c# - WCF关系

转载 作者:行者123 更新时间:2023-11-30 13:04:07 26 4
gpt4 key购买 nike

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

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

这是我的数据契约(Contract):

[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 类。

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

还有一个问题:如何通过 WCF IEnumerable of Estate 发送,其中每个 Estate 都包含 ICollection of Location?

最佳答案

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

[DataContract(IsReference=true]
public class Location

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

关于c# - WCF关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10436491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com