Can't get KnownType to work with WCF
Posted
by
Kelly Cline
on Stack Overflow
See other posts from Stack Overflow
or by Kelly Cline
Published on 2012-07-03T21:46:14Z
Indexed on
2014/06/12
3:26 UTC
Read the original article
Hit count: 250
wcf
|known-types
I have an interface and a class defined in separate assemblies, like this:
namespace DataInterfaces
{
public interface IPerson
{
string Name { get; set; }
}
}
namespace DataObjects
{
[DataContract]
[KnownType( typeof( IPerson ) ) ]
public class Person : IPerson
{
[DataMember]
public string Name { get; set; }
}
}
This is my Service Interface:
public interface ICalculator
{
[OperationContract]
IPerson GetPerson ( );
}
When I update my Service Reference for my Client, I get this in the Reference.cs:
public object GetPerson() {
return base.Channel.GetPerson();
I was hoping that KnownType would give me IPerson instead of "object" here.
I have also tried [KnownType( typeof( Person ) ) ]
with the same result. I have control of both client and server, so I have my DataObjects (where Person is defined) and DataInterfaces (where IPerson is defined) assemblies in both places. Is there something obvious I am missing? I thought KnownType was the answer to being able to use interfaces with WCF.
----- FURTHER INFORMATION ----- I removed the KnownType from the Person class and added
[ServiceKnownType( typeof( Person ) ) ]
to my service interface, as suggested by Richard. The client-side proxy still looks the same,
public object GetPerson() { return base.Channel.GetPerson();
, but now it doesn't blow up. The client just has an "object", though, so it has to cast it to IPerson before it is useful.
var person = client.GetPerson ( );
Console.WriteLine ( ( ( IPerson ) person ).Name );
© Stack Overflow or respective owner