Safi Mustafa
Safi Mustafa

Reputation: 175

Return Inherited Class into Base Class WCF Service

I am trying to return a derived class from the base class using WCF service, but I keep getting the following exception

"An error occurred while receiving the HTTP response to http://localhost:50137/Service.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server..."

I have tried adding all of the following over WCF Service method.

1) [XmlInclude(typeof(DerivedClass1)), XmlInclude(typeof(DerivedClass2))]

2) [SoapRpcMethod]

3) [SoapInclude(typeof(DerivedClass1)), SoapInclude(typeof(DerivedClass2))]

Code:

public class BaseClass
{
}
public class DerivedClass1:BaseClass
{
}
public class DerivedClass2:BaseClass
{
}

Wcf Service Method:

public BaseClass Validate()
{
    if(someCondition)
       return new DerivedClass1();
    else
       return new DerivedClass2();
}

Upvotes: 2

Views: 1449

Answers (2)

Fareed Ud Din
Fareed Ud Din

Reputation: 530

[Serializable]
[DataContract]
[
   KnownType(typeof(DerivedClass1)), 
   KnownType(typeof(DerivedClass2))
]
public class BaseClass
{
}
public class DerivedClass1:BaseClass
{
}
public class DerivedClass2:BaseClass
{
}

see https://msdn.microsoft.com/en-us/magazine/gg598929.aspx for more information about Known Types and the Generic Resolver.

Upvotes: 6

Tom W
Tom W

Reputation: 5403

There are a number of problems with the code you've posted:

  • Your contract type and operation code have no ServiceModel annotations
  • You haven't specified how you're hosting the service
  • You haven't specified how you're calling the service
  • You haven't specified anything about the binding you're using

Until at least some of these are clarified I think the question is unanswerable. If you could edit your question to include these points I'll edit my answer.

Upvotes: 0

Related Questions