Passing an Inherited Object to a WCF Service Using JSON

I have two classes that I listed below

public Class Vehicle { int wheels { get ; set} } public Class Car:Vehicle { int topspeed { get; set ;} } //This is the container class public Class Message { string ConatinerName { get; set;} Vehicle Container; } 

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 
+4
source share
2 answers

I have a similar problem using Python to call WCF with JSON. It is noteworthy that for me this was a guarantee that the __type key was the first in the mail request.

For example, json.dumps(data, sort_keys=True) will return something like this. WCF did not like this because __type not the first in Container . So my suggestion was to make sure __type is first. (Also, I am very surprised that sort_keys not recursive.)

Wrong:

 {"Container": {"Model": "El Camino", "TopSpeed": 150, "Wheels": 0, "__type": "Car:#WcfService1"},"WrapperName": "Car Wrapper"} 

On right:

 {"Container": {"__type": "Car:#WcfService1", "Model": "El Camino", "TopSpeed": 150, "Wheels": 0},"WrapperName": "Car Wrapper"} 

A simple python test client.

 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 
+4
source

Add this attribute to the Vehicle class.

[KnownType (TypeOf ("Auto"))]

0
source

Source: https://habr.com/ru/post/1342240/


All Articles