I have two classes that I listed below
public Class Vehicle { int wheels { get ; set} } public Class Car:Vehicle { int topspeed { get; set ;} }
I have defined a service contract that looks like this. This web service has two endpoints. One is SOAP and the other is Json
//this function gets a message object, looks into the container public Message GetMessage(Message Input) { Car mycar = (Car)Message.Container; mycar.topspeed = 200; Message retMessage = new Message(); retMessage.ContainerName ="Adjusted Car Speed"; retMessage.Container = mycar; return retMessage; }
When I start the WCF web service, my own visual studio test client can call the service with the Message object and provide the ability to transfer either the car or the vehcile object in the Message container. The VS client uses the soap endpoint according to the raw data that is being transmitted. To check json service endpoint
I am using a simple client written in Python that calls the aforementioned webservice GetMessage() method using the json data format. I pass the Car object, but when the service really gets
The webservice method gets data, the container inside the object contains only the Vehicle object. I looked at the request context that the web method receives, and it shows that the correct json string was received (since it was sent), but .net somehow removes the Car class property and only goes to the Vehicle property. Thus, a car throw inside GetMessage() throws an exception, saying that you are trying to direct the car to a car that is invalid.
Now I understand that the Container inside the Message is of type Vehicle , but for the SOAP endpoint, I can pass the car object and the vehicle object, but for the json endpoint, only the Vehicle Object can be transmitted through the Message container.
My question is: how can I make the .NET runtime recognize what I'm trying to pass to Car , not Vehicle ?
My json client code is below
import urllib2, urllib, json def get_json(url): f = urllib2.urlopen(url) resp = f.read() f.close() return json.loads(resp) def post(url, data): headers = {'Content-Type': 'application/json'} request = urllib2.Request(url, data, headers) f = urllib2.urlopen(request) response = f.read() f.close() return response geturl = 'http://localhost:24573/Service1.svc/json/GetMessage' sendurl = 'http://localhost:24573/Service1.svc/json/SendMessage' msg = get_json(geturl) print 'Got', msg print 'Sending...' retval = post(sendurl, json.dumps(msg)) print 'POST RESPONSE', retval