none
WCF Service doesn't work with List<List<object>> RRS feed

  • Question

  • I have a WCF service which gives back a custom type with my cell Values for a DataGrid (because I show dynamic data in a DataGrid)

        [DataContract]
        public class resultTable
        {
            [DataMember]
            public List<List<object>> myResults;
    ...
    }


            //[OperationContract]
            public resultTable getTable(customStruc struc, Dictionary<string,string> myCriterias)
            {
                resultTable myResultTable = new resultTable();
                myResultTable.myResults = new List<List<object>>();
    ...
    
                try
                {
                    using (DataClasses1DataContext db = new DataClasses1DataContext())
                    {
    ...
                        
                        string sql = "";
                        if (struc.SQLObjectType.ToLower() == "procedure")
                        {
                            sql = "execute " + struc.SQLObjectName + whereValues;
                            executeSQL(sql, db, myResultTable);
                        }
                        else if (struc.SQLObjectType.ToLower() == "function")
                        {                      
    
                            sql = "select * from " + struc.SQLObjectName + " (" + whereValues + ")";
                            executeSQL(sql, db, myResultTable);
                        }
                        else if (struc.SQLObjectType.ToLower() == "view")
                        {
                            sql = "select * from " + struc.SQLObjectName;
                            executeSQL(sql, db, myResultTable);
                        }
    ...
     
                    }
                }
                catch (Exception ex)
                {
                    myResultTable.Descriptions.Add(ex.Message); 
                }
                return myResultTable;
            }
    
    
    
            private void executeSQL(string sql, DataClasses1DataContext db, resultTable myResultTable)
            {
                using (DbCommand command = db.Connection.CreateCommand())
                {
                    command.CommandText = sql;
                    db.Connection.Open();
    
                    using (DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (reader.Read())
                        {
                            readRowValues(myResultTable, reader);
                        }
                    }
                }
            }
    
            private void readRowValues(resultTable myResultTable, DbDataReader reader)
            {
                object[] rowValues = new object[reader.FieldCount];
                reader.GetValues(rowValues);
    ...
                myResultTable.myResults.Add(rowValues.ToList());
            }


    If I go through my rowValues and cast them to string my WCF service can be used. If I leave objects (DateTime, Int32 etc) in it, it will throw a CommunicationException ("Not found") in my automatically generated Reference.cs at EndInvoke

                public Auswertungen.MyWCFService.resultTable EndgetTable(System.IAsyncResult result) {
                    object[] _args = new object[0];
                    Auswertungen.MyWCFService.resultTable _result = ((Auswertungen.MyWCFService.resultTable)(base.EndInvoke("getTable", _args, result)));
                    return _result;
                }

    The service method doesn't throw an exception, it returns a valid result.
    So why does this Exception appear?

    Tuesday, September 13, 2016 10:18 AM

Answers

  • Finally I found out by myself. Its no problem with List<object> as long as I don't store System.DBNull in it. If I replace all DBNull values with String.Empty I can return the List<object> with my string, int and DateTime values.

    In my service reference of the client part of the silverlight project I chose System.Collections.Generic.List as List-Type. Now my DataGrid shows the data correctly.

    • Marked as answer by Quidnunc1987 Tuesday, September 13, 2016 1:04 PM
    Tuesday, September 13, 2016 1:03 PM

All replies

  • First of all: Is there any need for List<List<object>>?? Such a generic type makes imho no sense. Why don't you use your custom type?

    And you should use an explicit row/column structure for a table.

    Then use abstraction aka. data layer. There should no direct interaction with any kind of SQL database.

    And finally: Please post a concise and complete example.

    Tuesday, September 13, 2016 10:50 AM
  • yes, theres a need. As I told, I want to bind dynamic data to a DataGrid, so I dont have a custom type. And in Silverlight I can't bind a table to my DataGrid.

    The only way I found around this problem would be to give back a DataTable and split in in the client part of the project.

    Even a "normal" List<object> makes problems, if I fill it with reader.GetValues(). Its no problem if its empty or filled with strings.

    Tuesday, September 13, 2016 11:17 AM
  • btw, how should that work with out using the [KnownType(typeof(List<List<string>>))] attribute?

    Even a "normal" List<object>[..]

    yup, object is not a normal data type for WCF serialization. There is no information in it for sending as message over WCF.

    Tuesday, September 13, 2016 11:34 AM
  • Finally I found out by myself. Its no problem with List<object> as long as I don't store System.DBNull in it. If I replace all DBNull values with String.Empty I can return the List<object> with my string, int and DateTime values.

    In my service reference of the client part of the silverlight project I chose System.Collections.Generic.List as List-Type. Now my DataGrid shows the data correctly.

    • Marked as answer by Quidnunc1987 Tuesday, September 13, 2016 1:04 PM
    Tuesday, September 13, 2016 1:03 PM
  • Finally I found out by myself. Its no problem with List<object> as long as I don't store System.DBNull in it. If I replace all DBNull values with String.Empty I can return the List<object> with my string, int and DateTime values.

    In my service reference of the client part of the silverlight project I chose System.Collections.Generic.List as List-Type. Now my DataGrid shows the data correctly.


    I find this questionable,  and it's something to be questioned particularly about object being somehow a knowntype to WCF.
    Tuesday, September 13, 2016 2:04 PM