Click here to Skip to main content
14,878,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I'm trying to build a WCF service that implements the following interface.
public interface IWebService
	void DoWork(BaseClass argument);

I also have the following classes:

public class BaseClass
    private string[] elements;

public class SuperClass : BaseClass
    public string Name
        get{return elements[0];}

But when I consume the WCF service with the following client, it results in an error:
static void Main()
    WebserviceClient _client = new WebserviceClient();
    SuperClass argument = new SuperClass();
    argument.Name = "Joe";

There was an error while trying to serialize parameter The InnerException message was 'Type 'SuperClass' with data contract name 'SuperClass:' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.

I know I could add the type 'SuperClass' to the list of knowntypes. But that is not my intention. I want to build a lot of wrapper classes around this baseClass that are easy to use for me/other users. But these wrapper classes are nothing more than BaseClasses.

The above example code is a simplified version of my real project.

Does anyone know what I have to do to make this work?

Kind regards,

1 solution

Hi This is a very known issue.
SOA is different than OOP with new mindset.
We, OOP developers need to change our way of thinking when we design our solution in SOA.

In short, the problem happens because you signed a contract that you commit to send an object BaseClass, but it was found out that you sent an object of another type that was unknown or unexpected by the very contract that you should adhere to.

To solve this problem, add the attribute "KnownType" above the definition of the DataContract or use "ServiceKnownType"..

This is the solution, Please find more online.. I hope that gives you a clue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900