First of all keep in mind, that I followed a very much stuff in google about this subject.
I am using WCF
, to exposing some services. I have something like:
First, your classes don't inherit from each other. I guess base should be the base class of sub? You will need to fix that first.
Then, the foo method has to be virtual to be overriden in sub. Fix that next.
And last but not least, WCF does not transport methods. It transports data only.
If you need executable code, put it into other classes. It's very confusing for users and yourself when you are tempted to think that your class will actually be transferred. It won't. Only it's data.
As I understand, you want to manually send correct XML to your service and make it recognize the Sub entity. In my comments to your question I mentioned approach with KnownTypes discovery, but it is solving another kind of problems.
Your current C# code looks ok, but you are missing some type attributes in the XML. It should look like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://tempuri.org/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:wcf="http://schemas.datacontract.org/2004/07/<PUT THE C# NAMESPACE OF YOUR ENTITIES HERE>">
<soapenv:Header/>
<soapenv:Body>
<tem:Test>
<tem:b xsi:type="wcf:Sub">
<wcf:base>1</wcf:base>
<wcf:sub>2</wcf:sub>
</tem:b>
</tem:Test>
</soapenv:Body>
</soapenv:Envelope>
I just tested it and it worked fine in a WCF project created with VS 2015 and tested with SoapUI. Both sub and base values reached the test method and Sub entity was available inside of it.
A similar question on StackOverflow with the same xsi:type solution also given here.
If this does not help enough, I can upload the demo project somewhere on GitHub.