Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WCF
Hi everyone,
 
I'm trying to build a WCF service that implements the following interface.
[ServiceContract]
public interface IWebService
{
	[OperationContract]
	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];}
        set{elements[0]=value;}
    }
}
 
But when I consume the WCF service with the following client, it results in an error:
static void Main()
{
    WebserviceClient _client = new WebserviceClient();
    _client.Open();
    
    SuperClass argument = new SuperClass();
    argument.Name = "Joe";
    
    _client.DoWork((BaseClass)argument);
}
 
There was an error while trying to serialize parameter http://tempuri.org/:argument. The InnerException message was 'Type 'SuperClass' with data contract name 'SuperClass:http://schemas.datacontract.org/2004/07/SharedTypes' 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,
Posted 17-Sep-12 22:35pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 21 Sep 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100