简体   繁体   中英

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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM