?
Solved

WCF Service won't pass back inherited class

Posted on 2011-09-29
4
Medium Priority
?
423 Views
Last Modified: 2012-05-12
Ok as we all know in OO programming one should program to the interface or to the abstract/base class.  I have created a service which has two methods one returning an instance of the base class and another returning an instance of a concrete class that inherits from the base class.  

To call these service methods, I created a client application.  The service method returning an instance of the base class works but the method returning an instance of the class which inherits from the base class doesn't (some sort of communication error or so it claims).  I tried using the KnownType Attribute to no avail.  What am I missing?  I have attached a zip file with the projects.  You will need to add a reference to the service since some filetypes are not allowed to be uploaded.

TIA,
Mo MyTest.zip
0
Comment
Question by:modotx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
theHollow earned 2000 total points
ID: 36818415
Hello
I still believe the solution is in the KnownType attribute.

KnownTyoe usage:
Lets's say I have a base class "MyClassBase", which has some properties.

I also have two classes which inherit the "MyClassBase" class.
Let's call them "MyClass1" and "MyClass2".

I have some service methods.
They looks like this in the service interface:
[OperationContract]
MyClassBase SomeMethod1();

[OperationContract]
MyClassBase SomeMethod2();

Open in new window


And it looks like this in my service class:
public MyClassBase SomeMethod1()
{
    return new MyClass1();
}

public MyClassBase SomeMethod2()
{
    return new MyClass2();
}

Open in new window


This will fail unless you put some KnownType attributes.

The KnownType attribute should be put on the base class.
Like this:
[KnownType(typeof(MyClass1))]
[KnownType(typeof(MyClass2))]
public MyClassBase
{
    public String SomeProperty { get; set; }
}

public MyClass1 : MyClassBase
{
    public Int32 SomeOtherProperty { get; set; }
}

public MyClass2 : MyClassBase
{
    public Boolean SomeDifferentProperty { get; set; }
}

Open in new window


Hope it helps :-)
0
 

Author Comment

by:modotx
ID: 36891394
It more than helped, it worked!  :)

All of the examples I was looking at did not explain clearly where the KnownType attribute should be placed with the base class and that it only applied to return types of methods.  As a matter of fact, some said it wasn't necessary to to use the KnownType.

I wonder why the serializer can't or isn't built with enough smarts to deduce the inheritance tree.  Is it just about performance?

Thank you so much though.
0
 

Author Closing Comment

by:modotx
ID: 36891396
Thanks again!
0
 
LVL 4

Expert Comment

by:theHollow
ID: 36891648
Hehe
No problem :-) I have been fighting the same problem before.

So KnownType is highly necessary. It only works a little different way than one might think is logical at first.
I don't know why it's like that, but I bet it's about performance and to reduce the number of bytes to send over the internet.

Wishing a good week-end :-)
0

Featured Post

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question