WCF服务我有我已经定义,它看起来像如下所示的服务合同,我下面传递继承的对象使用JSON
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;
}
列出的两个类。这个webservice有两个端点启用。一个是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 Web服务,视觉工作室本地测试客户端能够与Message对象调用服务,并提供在任何汽车或vehcile传递选项对象在Message容器中。该VS客户端使用SOAP端点按该被传递的原始数据。 为了测试服务的JSON端点
我使用Python编写一个简单的客户端使用JSON数据格式,调用上面的web服务GetMessage()
方法。我传入一个Car
对象,但是当服务实际得到时
webservice方法获取数据,对象内的容器只包含Vehicle对象。 我检查了webmethod接收到的请求上下文,它显示接收到正确的json字符串(因为它已发送),但.net以某种方式剥离Car
类属性,并且只传递Vehicle
属性。所以在GetMessage()
内部的车辆投射出一个异常情况,说你正在试图将车辆投掷到一辆无效的车辆上。
现在我明白了Container
内Message
为Vehicle
型的,但对于SOAP终点,我能够在汽车对象和车辆对象通过,但对于JSON终点只有一个Vehicle对象可以传递通过Message
容器。
我的问题是我如何让.NET运行时识别出我想通过Car
而不是Vehicle
?
我的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
我想这和它没有工作 – CrazycodeMonkey 2011-03-04 16:09:17
你的意思[KnownType(typeof运算(车))] 它帮助我,当我面临同样的问题,消耗来自本地客户端的WCF服务 – SKandeel 2012-06-25 16:24:25