Reputation: 41
Here what my Problem and requirement is.
// Below is my List of one Class function i.e is User, i am having 10-15 more class functions
public IEnumerable<Entity.User> User()
{
OpenStoredPorcedure("spDB");
DataSet d = ExecuteDataSet();
DataTable myDataTable = d.Tables[0];
var stList = new List<Entity.User>();
foreach (DataRow dr in myDataTable.Rows)
{
Entity.User usr = new Entity.User()
{
FirstName = dr["first_name"].ToString() ?? null,
LastName = dr["last_name"].ToString() ?? null
};
stList.Add(usr);
}
return stList.AsEnumerable();
}
Now in WCF i want one function like below one
public IEnumerable fetchData(int id)
{
IEnumerable result1 = null;
switch (id)
{
case 1:
IEnumerable<Entity.User> result = User().AsEnumerable<Entity.User>().ToArray();
result1 = result;
break;
case 2:
IEnumerable<Entity.Project> result = User().AsEnumerable<Entity.Project>().ToArray();
result1 = result;
break;
}
return result1;
}
But i am getting this error when using above method
Request Error The server encountered an error processing the request. The exception message is 'Cannot serialize parameter of type 'System.Collections.Generic.List`1[Entity+User]' (for operation 'fetchData', contract 'ISyncService') because it is not the exact type 'System.Collections.IEnumerable' in the method signature and is not in the known types collection. In order to serialize the parameter, add the type to the known types collection for the operation using
Now i want that in WCF we call this function and pass on the id its will result json list of that Entity Class.
I am getting Json data when i am using below function
public List<Entity.User> fetchData()
{
return User().ToList();
}
Upvotes: 4
Views: 1357
Reputation: 16013
In a case you have a base class and inheritance between the types you return you should add a KnownType attribute on your base class datacontract and then specify all derived types in the attribute
Something like this
[KnownType(typeof(YourDerivedType))]
Here is some help : http://blogs.msdn.com/b/sowmy/archive/2006/06/06/all-about-knowntypes.aspx
In a case you're returning a types not related to each other, you should do somethig as suggested by OsQu.
Upvotes: 1
Reputation: 1058
I think the probelm is that you need to give type to IEnumerable.
If you know the type in each table, you can create generic method and supply it with correct type.
public IEnumerable<T> fetchData<T>(int id)
{
IEnumerable<T> result1 = null;
switch (id)
{
case 1:
IEnumerable<T> result = sync.User() as IEnumerable<T>;
result1 = result;
break;
}
return result1;
}
And then use it:
IEnumerable<Entity.User> retVal = fetchData<Entity.User>(1);
More info about generics: Generics C#
Upvotes: 0