0

I have a question about working with DB using Liqn to SQL.

I have a little database, which includes 4 tables and I have Liq to SQL classes created for this tables: Class Diagramm So, It's all created on my AppName.Web project. I'm also created a WPF Service, that returns List collection with all Entries inside. When I just tried to call my function from Silverlight project I've got CommunicationException. I've tried to set maxReceivedMessageSize and maxBufferSize, but problem still appeared. 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.

How can I receive List of Mods with all childs in all tables? And is linq to sql one of the best and easiest ways to do it?

UPD1: Functions to return List:

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();
}
Iworb
  • 522
  • 7
  • 29
  • The problem is that your code is not well layered. The WCF service should be in the middle tier separate from the data access layer. And the UI should be separate from the middle tier. – Stewart Mbofana Jan 07 '14 at 12:28
  • When creating SL application you have 2 projects: one for data and services and one for UI. I've tried to get data from AppName.Web to AppName project (like in all SL projects) – Iworb Jan 07 '14 at 12:35

1 Answers1

0

CommunicationException is exceptionally broad. Can you create a simple operation on the WCF service to ensure its reachable and that you don't have a configuration / hosting issue?

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)

Also make sure you're materializing the result of your Linq2Sql query before returning it from the WCF service.

As to choice of object relationship mapper (ORM) Linq2Sql is deprecated in favor of 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. 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 } } });

etc.

One more note: EntityFramework allows the option to turn off dynamic proxies but I don't know if Linq2Sql does. (As a comparison point between the two)

Kevin
  • 429
  • 3
  • 10
  • Like I said before, I created function to get List of Mods without any relations (without `Items` field) and it works well. – Iworb Jan 07 '14 at 13:30
  • I tried to use ADO.NET EDM, but I've received the same error. On debug mode I can see what my service function returns, but I can't receive it on the client. Error appers here: `ObservableCollection _result = ((ObservableCollection)(base.EndInvoke("GetMods", _args, result)));` in the service reference file. – Iworb Jan 07 '14 at 14:12
  • Since the problem is with a related set I'm still thinking this is a materialization problem. If you use a DataLoadOption (http://stackoverflow.com/questions/5783109/in-linq-to-sql-how-do-i-include-the-child-entity-with-initial-query) to load the related set with the initial query does it work? – Kevin Jan 07 '14 at 15:48
  • In the debug mode I'll saw what function returns. There was all I want (all `mods` with all related data). I'll try DataLoadOption and than comment here. – Iworb Jan 08 '14 at 07:32
  • I tried to use DataLoadOption, but problem still appears. What you mean about materizlizing the result? I add my functions to the main text. – Iworb Jan 09 '14 at 11:57
  • Materializing a result means converting a LINQ queryable expression to a concrete representation which you're doing with the ToList() for Mod and now doing for the related sets with the LoadWith options. Seeing the svctrace is the only thing I can think of to further debug the issue – Kevin Jan 09 '14 at 13:19
  • I think, that problem related with "Navigation properties". **I remake my solution to work with ADO.NET EDM.** OK. svctrace will be as soon as possible – Iworb Jan 09 '14 at 14:08
  • I tried to add logging to my configuration, but it don't creates any logs at all... – Iworb Jan 09 '14 at 14:23
  • Need to see your configuration. Also make sure your application / user has permission to write to the log file path. – Kevin Jan 09 '14 at 15:04
  • Here (http://pastebin.com/53ScJaNE) is my web.config. To test write permissions I add to function some `FileStream` and `StreamWriter` and tried to wtite to "C://file.txt" and it works. – Iworb Jan 09 '14 at 15:14
  • Rough guess but I don't believe you need the filter element. Since it's empty that might mean nothing will match. You can also try http://davintryon.blogspot.co.uk/2012/03/wcf-tracing-raw-soap-xml.html – Kevin Jan 09 '14 at 15:30
  • So I changed config like in link above ant nothing happens. As you know, I'm receive error not in XML or any other format, but in new window. Sorry for different language. http://smages.com/images/2014010917.jpg – Iworb Jan 09 '14 at 15:39
  • Below link to the .svclog file. This file contains only GetMods() function call and logs for it. I've tried to use neutral culture in my log file, but my native language still appears... http://www.mediafire.com/view/eh3d1g68h78bx6f/Messages.svclog – Iworb Jan 09 '14 at 15:52
  • Was able to paste from the log into Bing Translate and update my answer. – Kevin Jan 09 '14 at 16:11
  • Translators can't parse so long file, sorry. I tried to add [DataContract] and [DataMember] attributes to recenly used Linq-to-sql classes, but it didn't work. It worked only when I didn't include **related** data (like Items in Mod or etc.) I think, that automappers won't add this **related** data, but I have some idea to add it by myself on the Service side. – Iworb Jan 09 '14 at 16:32