简体   繁体   English

当我从Silverlight调用WCF时发生CommunicationException(或者如何在一个函数中从DB中获取所有数据)

[英]CommunicationException when I call WCF from Silverlight (or how to get ALL data from DB in one function)

I have a question about working with DB using Liqn to SQL. 我有一个关于使用Liqn to SQL处理数据库的问题。

I have a little database, which includes 4 tables and I have Liq to SQL classes created for this tables: 我有一个小的数据库,其中包括4个表,并且我为此表创建了Liq to SQL类: 类图 So, It's all created on my AppName. 因此,所有这些都在我的AppName上创建。 Web project. Web项目。 I'm also created a WPF Service, that returns List collection with all Entries inside. 我还创建了WPF服务,该服务返回包含所有条目的List集合。 When I just tried to call my function from Silverlight project I've got CommunicationException . 当我刚尝试从Silverlight项目中调用函数时,遇到了CommunicationException I've tried to set maxReceivedMessageSize and maxBufferSize , but problem still appeared. 我尝试设置maxReceivedMessageSizemaxBufferSize ,但问题仍然出现。 Than I add [DataContract] and [DataMember] tags to auto-generated classes (except properties, that represents references, like Items in Mod table, Recipes and Entries in Item table, etc.) and it started to work, but I can receive only data from one table. 比我添加[DataContract][DataMember]标签自动生成的类(除了属性,代表引用,就像ItemsMod表, RecipesEntriesItem表等),并开始工作,但我可以接受仅来自一个表的数据。

How can I receive List of Mods with all childs in all tables? 如何接收所有表中所有子项的Mod列表? And is linq to sql one of the best and easiest ways to do it? linq to sql是执行此操作的最佳和最简单的方法之一吗?

UPD1: Functions to return List: UPD1:返回列表的函数:

public MCDataClassesDataContext GetContext()
{
    var context = new MCDataClassesDataContext();
    var dlo = new DataLoadOptions();
    dlo.LoadWith<Mod>(p => p.Item);
    dlo.LoadWith<Item>(p => p.Recipe);
    dlo.LoadWith<Item>(p => p.Entry);
    dlo.LoadWith<Recipe>(p => p.Entry);
    context.LoadOptions = dlo;
    return context;
}

public List<Mod> GetMods()
{
    return GetContext().Mod.ToList();
}

CommunicationException is exceptionally broad. CommunicationException异常广泛。 Can you create a simple operation on the WCF service to ensure its reachable and that you don't have a configuration / hosting issue? 您可以在WCF服务上创建一个简单的操作以确保其可访问并且没有配置/托管问题吗?

Alternately turn on WCF message logging and use svctraceviewer to gain better insight to your root cause for the CommunicationException ( http://msdn.microsoft.com/en-us/library/ms730064(v=vs.110).aspx ) 或者打开WCF消息日志记录,并使用svctraceviewer更好地了解CommunicationException的根本原因( http://msdn.microsoft.com/zh-cn/library/ms730064(v=vs.110).aspx

Also make sure you're materializing the result of your Linq2Sql query before returning it from the WCF service. 另外,在从WCF服务返回Linq2Sql查询的结果之前,请确保您已实现该查询的结果。

As to choice of object relationship mapper (ORM) Linq2Sql is deprecated in favor of EntityFramework. 关于对象关系映射器(ORM)的选择,不赞成使用Linq2Sql,而建议使用EntityFramework。 It still works, but much like the appendix in humans it's an evolutionary dead end. 它仍然有效,但是很像人类的附录,它是进化的死胡同。


After reviewing the stack trace I've been able to determine that WCF is unable to serialize the related types because Linq2Sql is emitting them as dynamic proxies and not as types for which you have marked as data contracts. 在检查了堆栈跟踪之后,我已经能够确定WCF无法序列化相关类型,因为Linq2Sql会将它们作为动态代理而不是作为您已标记为数据协定的类型发出。 I would recommend doing a mapping either manually or with something like Automapper which has an effect like this before returning from your service Select(i => new Mod() { Items = i.Items.Select((j) => new Item() { Foo = j.Foo } } }); 我建议您手动或使用Automapper之类的映射进行此操作,该映射具有这样的效果,然后再从您的服务返回Select(i => new Mod() { Items = i.Items.Select((j) => new Item() { Foo = j.Foo } } });

etc. 等等

One more note: EntityFramework allows the option to turn off dynamic proxies but I don't know if Linq2Sql does. 还有一点需要注意:EntityFramework允许关闭动态代理的选项,但是我不知道Linq2Sql是否支持。 (As a comparison point between the two) (作为两者之间的比较点)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM