Shantanu Gupta
Shantanu Gupta

Reputation: 21188

WCF service not generating classes for run time polymorphism

I created my base class A with several inherited class A1, A2, A3 and so on.

Now, using WCF service I am trying to return List which holds objects that can be of type A1, A2 or even A.

Do WCF supports this? I am getting connection closed error continuously.

eg.

class A{
//do something
}

class B:A{
//do something
}

class C:A{
//do something
}

WCF Service calling GetAll method which returns

public List<A> GetAll()
{
var obj= new List<A>();
obj.Add(new B());
obj.Add(new C());
return obj;
}

Now WCF service only have a knowledge about A but not B and C. How can I still return this object. As per oops this is valid but I don't know about service

Upvotes: 0

Views: 204

Answers (1)

Shantanu Gupta
Shantanu Gupta

Reputation: 21188

I was able to fix this by adding KnownType attribute to my class that is being exposed by service.

http://msdn.microsoft.com/en-us/library/ms730167.aspx

KnownType resolves serialization at runtime

[KnownType(typeof(B))]
[KnownType(typeof(C))]
class A{
//do something
}

class B:A{
//do something
}

class C:A{
//do something
}

Upvotes: 2

Related Questions