user990635
user990635

Reputation: 4249

Access inherited class in a web service

I have a web service project that contains 3 classes:

In my project (Test.csproj), I added a web reference to the above mentioned web service, and named it DAL.

When I'm trying to access for example "BaseClass" from my Test project, I write Test.DAL.BaseClass, and it recognizes it.

When I try to do the same thing to access any of my inherited classes (InheritedClassA and InheritedClassB) from my Test project it doesn't work.

Upvotes: 2

Views: 1721

Answers (2)

Kirk Broadhurst
Kirk Broadhurst

Reputation: 28708

Are you using WCF for your web services?

If you're using DataContractSerializer (which you probably will be if you're using WCF), then you want to use the KnownType attribute to include types in the service description.

Data Contract Known Types

[DataContract]
[KnownType(typeof(InheritedClassA))]
[KnownType(typeof(InheritedClassB))]
public abstract class BaseClass
{
    // properties
}

Web-services are not really the same as WCF but I find that a lot of people use the terms interchangably.

Upvotes: 1

Roland Mai
Roland Mai

Reputation: 31077

You need to tell the service to use the classes InheritedClassA and InheritedClassB either by returning instances of those classes in methods, or use XmlInclude to include the unused classes in the reference. Make sure to update your web reference after you make any of these changes.

Upvotes: 2

Related Questions