2011-03-04 209 views
4

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()內部的車輛投射出一個異常情況,說你正在試圖將車輛投擲到一輛無效的車輛上。

現在我明白了ContainerMessageVehicle型的,但對於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 

回答

4

我有使用Python有類似的問題使用JSON調用WCF。值得注意的是,爲我確定的是確保__type密鑰在發佈請求中排名第一。

例如,json.dumps(data, sort_keys=True)會返回類似這樣的內容。 WCF服務不喜歡那樣,因爲__type並不是Container中的第一個。所以,我的建議是確保__type是第一個。 (另外,我很驚訝,sort_keys不是遞歸。)

錯誤:

{"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 
0

這個屬性添加到汽車類

[KnownType(typeof運算( 「汽車」))]

+0

我想這和它沒有工作 – CrazycodeMonkey 2011-03-04 16:09:17

+0

你的意思[KnownType(typeof運算(車))] 它幫助我,當我面臨同樣的問題,消耗來自本地客戶端的WCF服務 – SKandeel 2012-06-25 16:24:25