Reputation: 4035
How do I perform polymorphism in a scenario like this:
if I have a class Dachshund in namespace Foo that is inherited from class Dog in namespace Bar:
namespace Bar
{
public class Dog
{
public string Color { get; set; }
public string Bark { get; set; }
}
}
namespace Foo
{
public class Dachshund : Dog
{
public int Length { get; set; }
}
}
and I use Dachshund to do some dog stuff but pass it through wcf as a dog, what gives? some serialization issues:
[ServiceBehavior]
public class myWcfService : ImyWcfService
{
[OperationBehavior]
public Message GetDog()
{
var myDach = new Dachshund();
// do some stuff with my dach
return new Message { Dog = myDach as Dog, Message = "I'm sending a dog" };
}
}
public class Message
{
public Dog Dog { get; set; }
public string Message { get; set; }
}
Upvotes: 0
Views: 140
Reputation: 44048
You need to add the [ServiceKnownType]
attribute to your service contract (the interface that defines the service operations, rather than the service implementation (the actual class):
[ServiceContract]
[ServiceKnownType(typeof(Dachshund))] // <- Add this attribute
public interface ImyWcfService
{
//...
}
this will be enough for the DataContractSerializer to "know" your class and be able to properly serialize / deserialize it.
Upvotes: 1