Can't get KnownType to work with WCF
- by Kelly Cline
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 );