WCF 关系

Posted

技术标签:

【中文标题】WCF 关系【英文标题】:WCF Relationship 【发布时间】:2012-05-13 06:28:32 【问题描述】:

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

继承自 Advert 类的 Estate 类,而 Locations 与广告相关(广告属于某些地点的多对多关系)

这是我的数据合同:

[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)

我也在 E​​F 中将此类用作 POCO 类。

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

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

【问题讨论】:

您写了这个问题,并认为实际上告诉我们错误是什么没有帮助? 您需要告诉我们错误是什么,并检查服务器是否发生错误。它与消息的大小有关吗?您可以尝试检查 increasing the message size allowed 是否有帮助。 (您发布的实际错误并不能告诉我们太多) 也许您可以发布实际的异常消息,并可能对它的翻译做出最好的猜测? 启用 WCF 跟踪 (msdn.microsoft.com/en-us/library/ms732023.aspx) 这样您会发现“更好”的异常 您的服务正在生成异常。为了找到您必须打开 wcf 跟踪的异常情况,该跟踪将生成 .trace 文件,这些文件可以通过 windows 中的内置工具读取,或者您可以在 Web 配置中打开跟踪( ) 并通过输入服务的 url 后跟 /trace.axd (例如 myserviceurl.com/trace.axd) 来查看异常 【参考方案1】:

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

[DataContract(IsReference=true]
public class Location

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

【讨论】:

以上是关于WCF 关系的主要内容,如果未能解决你的问题,请参考以下文章

WCF 操作名称和操作之间的关系

WCF:NetworkCredential 和 Impersonation 之间的关系

SVC 文件和 WCF 项目之间的关系?

自定义 wcf 数据提供者和调试关系错误

WCF 服务无法为具有权限的 SSL/TLS 安全通道建立信任关系

WCF:无法为 SSL/TLS 安全通道建立信任关系,出现权限错误