C# 使用JSON将继承的对象传递给WCF服务

C# 使用JSON将继承的对象传递给WCF服务,c#,python,json,wcf,soap,C#,Python,Json,Wcf,Soap,我在下面列出了两门课 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 = {'C

我在下面列出了两门课

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
公共级车辆
{
int轮{get;set}
}
公营车辆
{
int topspeed{get;set;}
}
//这是容器类
公共类消息
{
字符串conatintername{get;set;}
车辆集装箱;
}
我定义了一个服务合同,如下所示。此Web服务启用了两个端点。一个是SOAP,另一个是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;
}
当我运行WCF webservice时,visual Studio本机测试客户端能够使用消息对象调用该服务,并提供了在消息容器中传递car或vehcile对象的选项。VS客户端根据传入的原始数据使用soap端点。 测试服务的json端点

我使用的是一个用Python编写的简单客户端,它使用json数据格式调用上述webservice
GetMessage()
方法。我通过一辆
汽车
物体,但当服务真正得到

webservice方法获取数据,对象内的容器仅包含Vehicle对象。 我已经检查了webmethod接收的请求上下文,它显示接收到正确的json字符串(在发送时),但是.net以某种方式剥离了
Car
类属性,只传入
Vehicle
属性。因此,cast to car inside
GetMessage()
抛出一个异常,表示您试图将车辆强制转换为无效的强制转换

现在我知道
消息中的
容器
属于
车辆
类型,但是对于SOAP端点,我可以传入一个car对象和一个Vehicle对象,但是对于json端点,只有一个Vehicle对象可以通过
消息
容器传入

我的问题是,如何使.NET运行时识别我试图乘坐的是
汽车
,而不是
汽车

我的json客户端代码发布在下面

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

将此属性添加到车辆类


[KnownType(typeof(“Car”)]

我在使用Python调用带有JSON的WCF时遇到了类似的问题。值得注意的是,对我来说,解决这个问题的方法是确保post请求中的
\u type
键排在第一位

例如,
json.dumps(data,sort\u keys=True)
将返回如下内容。WCF服务不喜欢这样,因为
\u type
不是
容器中的第一个。所以,我的建议是确保_类型是第一位的。(另外,我很惊讶,
排序键
不是递归的。)

错:

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

{"Container": {"__type": "Car:#WcfService1", "Model": "El Camino", "TopSpeed": 150, "Wheels": 0},"WrapperName": "Car Wrapper"}
简单的python测试客户端

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
你的意思是[KnownType(typeof(Car))]当我在使用本地客户端的WCF服务时,它帮助了我