9

I've been designing web services for quite a while now but never had to expose a 'complicated' WCF service until recently. I was baffled at the apparent lack of "proper support" in WCF for abstract types. Sure - you can USE them - sure you can get them to 'work'... you just don't end up with what you WANT...

The first problem is that if you generate code from a wsdl with an abstract type you get vastly different code because it falls back to the xmlserializer and not the DataContractSerializer. This is obviously a bit less than desirable... I'd like to use the fancy new faster serializer please thank you... (and all that comes along with Service/DataContract)

on the flip side - if you start with code first and expose a properly attributed abstract wcf class as a web service the exposed wsdl does NOT contain the abstract="true" attribute making the "abstract class" technically concrete... This is not what I want of course...

I've got a workaround but it involves a crazy amount of 'hackery' where I create the wsdl/xsd contract first, remove any abstract="true" (oh - let's not mention that I can't use attributes in the xsd shall we) and then svcuitl the result... But now I'm left with a c# api that has a CONCRETE abstract class and I then need to go modify that to ADD the abstract keyword... This 'works' but it's a huge pita - and not easily 'scriptable'...

This is all just whacked! I'm hoping someone can explain to me precisely 'why' this is... I welcome answers that don't cite "solid" resources but I'm really waiting for the person to tell me - with proper documentation (like preferably from good ol Don Box himself) why exactly this is... Cause I just don't get it...

Thanks all - if anyone would like more details - please let me know!

UPDATED FOR ADDITION OF A SAMPLE REQUEST - starting with c#

[ServiceContract]
public interface IShapeTest
{
  [OperationContract]
  AbsShape EchoShape(AbsShape shape);
}

public class ShapeTestImpl : IShapeTest
{
  public AbsShape EchoShape(AbsShape shape)
  {
    return shape;
  }
}

[KnownType(typeof(Square))]
public abstract class AbsShape
{
  [DataMember]
  public int numSides;
}

public class Square : AbsShape
{
  public Square() : base()
  {
    numSides = 4;//set the numSides to 'prove' it works
  }
}

EXPECTED TYPE:

<xs:complexType name="AbsShape" abstract="true"> <!--NOTE abstract="true"-->
  <xs:sequence>
    <xs:element minOccurs="0" name="numSides" type="xs:int"/>
  </xs:sequence>
</xs:complexType>

ACTUAL EMITTED TYPE:

<xs:complexType name="AbsShape"> <!--NOTE the lack of abstract="true"-->
  <xs:sequence>
    <xs:element minOccurs="0" name="numSides" type="xs:int"/>
  </xs:sequence>
</xs:complexType>
2
  • Perhaps you'd provide an example? I don't seem to be having the 'trouble' you are having. May 14, 2009 at 16:11
  • sample posted (more characters to meet the 15 char limit - lol)
    – dovholuk
    May 15, 2009 at 11:49

5 Answers 5

5

Well it's because WCF doesn't pass objects, it passes messages. This isn't remoting, so the type end up with on the client is not the same type you have on the server - it's simply a holding class for various properties. Implementing "abstract="true" simply makes no sense. Messages are just data - how would the client know what concrete type to use, as you're not sharing classes, but simply a representation of the message.

5
  • 1
    thanks for your response and i've seen this sort of answer on the internet but I don't believe it's "correct". I say this because even though it's passing messages those messages can still be polymorphic. if polymorphism was an issue I would expect that the DataContractSerializer wouldn't accept ANY object that derived from any other (well maybe ecluding object). The really odd thing is that if you hand craft your OWN wsdl that uses has 'abstract=true' - WCF has no issues and it works just fine... the PROBLEM is that the emitted type is inconsistent... imho
    – dovholuk
    May 15, 2009 at 11:52
  • Messages really aren't polymorphic (unless your method accepts and returns a single type, Message) Sure an object which has a message contract applied do it can be polymorphic but it's life ends at the boundary of the service. So yes, you can get the abstract=true attribute for a complex type, but then how does the client know what concrete type is supposed to be used, unless you're getting into sharing implementation libraries, something SOA is supposed to separate us from.
    – blowdart
    May 15, 2009 at 12:49
  • The client knows what type to be used by the xsi:type that is supplied in the xml... the same way the XmlSerializer does it... The only crazy part of this for me is that the XmlSerializer will handle this properly, the DataContractSerializer will also deserialize the xml properly but the DataContractSerializer just won't expose the type as abstract to start... I find it odd...
    – dovholuk
    May 15, 2009 at 20:24
  • 3
    I've read this with great interest and I think you're missing the fundamental point that WCF has been created to be platform agnostic. Abstraction/Polymorphism is an OO concept, and as such is not implemented in all languages. Seeing as in theory this service could be consumed by any language, including ones that don't support OO principles, you would be making this service incompatible to those languages.
    – John Mc
    May 12, 2011 at 15:05
  • 1
    Thanks for the comment. I respectfully disagree because while it's true that all languages might not be able to understand concepts from other languages that is entirely the point of xml schema. XML schema is entirely agnostic of platform. There is no requirement that a different stack understand the idea of 'abstract'. that platform can blindly ignore it if it so chooses (but i argue that it should at least throw a runtime error). MY point is that C# IS OO - so one would certainly think that WCF would respect abstract types and generate abstract classes. thanks again for your comment!
    – dovholuk
    Jul 13, 2011 at 2:13
1

"abstract" is an implementation detail. It has no place in a service contract. Naturally, as soon as you insist that your caller must be aware you are returning an abstract type, WCF must fall back on a serializer that can expose this fact: and you're back to being stuck with the XmlSerializer.

I suspect you're using abstract because that's the OO way to do things. But you're not doing OO, you're doing web services - SOA. There's a difference.

7
  • thanks for your comment... my question - similar to the one i commented below - is why would WCF support ANY kind of polymorphic behavior then? side question - how does one do a 'choice' with WCF DataContractSerializer... It seems that the 'answer' is "you can't"... and i think that is 'wrong' - no?
    – dovholuk
    May 15, 2009 at 11:56
  • 3
    No, the question is why are you trying to be explicit about your use of XML in data contracts. Recall that WCF does binary, JSON, Atom, etc, in addition to XML. xs:choice is an implementation dependency you should steer away from. Just keep your contracts to the semantics, and away from specifics of implementation. May 15, 2009 at 12:17
  • let me try again. I hear where you're coming from so let me ask the question like this... "how can i specify a data contract that allows me to send an Apple data structure or an Orange data structure"? I didn't explicitly reference xs:choice because I didn't want to add confusion but now it's time do do so... Using schema I can do this in 1 of 3 ways. a.) "xsd polymorphism" (cleaner OO code - easier to maintain imo) b.) xs:choice (clearer xml - generally uglier OO code - xsd harder to maintain) c.) substitution groups ("close enough" to be the same as choice) so how do i do that with WCF?
    – dovholuk
    May 16, 2009 at 2:04
  • Ok, that's a lot more clear. I don't have the details in front of me, but you do want to use [KnownTypes]. May 16, 2009 at 3:02
  • yes I do want to use KnownTypes - I have to. [btw. "KnownTypes" is another pet peeve of mine with the .NET "service" stack - but that's not relevant to THIS discussion ;) ] The real question is that if I expose an abstract c# class it 'becomes' a concrete class in the wsdl - and this is 'wrong'... I'm merely trying to convey to the consumer "pick one of these things but not the parent" (hence the abs. type) yet when it's exposes as concrete - there's nothing 'telling' the consumer that the parent is NOT valid - in fact they should believe that it IS valid. thx for cont. the conversation.
    – dovholuk
    May 18, 2009 at 12:36
1

The premise of this question is that WCF should map an XSD complexType element with an abstract attribute set to true as a C# abstract class. If you look at the WSDL spec on future extensibility, you'll see an example of how the spec uses this XSD feature to allow the addition of "element items" without needing to modify the WSDL spec itself.

The purpose of the XSD abstract attribute is to allow other XSD "types" to inherit definitions from the base type. This may sound like what @dovholuk is looking for WCF to do, but in actually it is a device for XSD type definition. This XSD feature has no bearing on the serialization of XML into C# class constructs since it is intended to be use within XSD definitions. So summarizing both @blowdart & @JohnSaunders, WCF is an abstraction over message exchange of which XSD-based type definition is an implementation detail.

3
  • I think you missed the point entirely. The spec you cite is unrelated (from my perspective). Look at w3.org/TR/xmlschema-0/#abstract. "When an element or type is declared to be "abstract", it cannot be used in an instance document." this maps perfectly to the same concept in OOP. C# is an OO language and as such I don't understand WHY WCF won't do what seems totally natural to me. thanks for your comment/answer though
    – dovholuk
    Aug 20, 2011 at 1:50
  • I believe the true answer to your question is the dreaded is it is by design. That WSDL utilizes XSD for data contract description does not imply a framework is required to implement XSD constructs such as type inheritance to be compliant. I think the point you're failing to consider is that WCF is an messaging abstraction. It was not created to be an implementation in a .NET language of the XSD spec. In fact, soap/WSDL is just on of several messaging protocol supported by WCF. Please consider this dead horse beaten :)
    – Sixto Saez
    Aug 22, 2011 at 12:22
  • @dovholuk I think I can see WHY: It simplifies the task of implementing a unified programming model for services, detached from bindings (and endpoints and transports). The only problem is that this leads directly to the stark choice between simply letting go of abstraction (a huge price to pay) OR accepting that your contracts systematically lie (if you use abstract types as inputs).
    – The Dag
    Mar 21, 2013 at 9:15
1

The attribute "abstract=true" on abstract types as well as a type's inheritance is preserved in the WSDL when you use Java EE. Any use of traditional Java keywords like "abstract" or "extends" in object models is preserved in the WSDL and the XSDs. No special attributes or mangling required.

WCF services fail to generate the abstract attribute in the WSDL. WCF also fails to traverse object model inheritance, requiring the KnownTypeAttribute to be used in order for the proper XSD inheritance to result in the WSDL.

WCF clients however will generate abstract types off the WSDL from a service that describes some types as abstract as well as preserve the inheritance for any type (using the "Service Reference" in Visual Studio to setup a client to a Java EE service for example).

So WCF honors abstract types defined in a WSDL for the client proxy but it does not create the abstract types in the service for WSDL creation (without some special attention perhaps, like the KnownTypeAttribute needed for inheritance).

0

Use [XmlSerializerFormat] Attribute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.