2012-03-26 38 views
-1

我想通過我的asmx web服務運行在.net 4.0上發送自定義類的對象,但我得到的是一個空的響應。見下文:ASMX web服務不返回任何結果

<soap:Body> 
    <ActivateResponse xmlns="http://tempuri.org/"> 
     <ActivateResult />        <!-- why is this empty --> 
    </ActivateResponse> 
</soap:Body> 

然而,如果我從類修改我的方法和改變返回類型例如甲,則它返回對象正確的所有屬性。見下:

<ActivateResponse xmlns="http://tempuri.org/"> 
    <ActivateResult> 
     <BtAddress>string</BtAddress> 
     <Name>string</Name> 
     <Number>string</Number> 
    </ActivateResult> 
</ActivateResponse> 

我想知道爲什麼它發生?我可以責怪不正當序列號A但沒有什麼幻想我參與我的課堂文件。兩個類文件在內容方面幾乎相似,並且不包含任何Serialize屬性。

那麼,爲什麼web服務返回一種類型,而不是另一種?


A類

public class A 
{ 
    private string code; 
    private bool isValid; 
    private int maxUniqueActivations; 
    private DateTime dateAdded; 
    private Customer customer = null; 
    private bool _initAsEmpty = false; 


    public License() 
    { 
     _initAsEmpty = true; 
    } 

    public string LicenseCode 
    { 
     get { return code; } 
     //set { code = String.IsNullOrWhiteSpace(value)? null : value.Trim(); } 
    } 
    //If i change return type to Customer, it works too 
    //so i dont think it should be blamed 
    public Customer Customer 
    { 
     get { return customer; } 
    } 
    public bool IsValid 
    { 
     get { return isValid; } 
    } 
    public int MaxUniqueActivations 
    { 
     get { return maxUniqueActivations; } 
    } 
    public DateTime DateAdded 
    { 
     get { return dateAdded; } 
    } 
} 

B類

public class Phone 
{ 
    private string btAddress, name, number; 
    private bool isValid; 
    private DateTime dateAdded; 
    private bool _initAsEmtpy = false; 

    public Phone() 
    { 
     _initAsEmtpy = true; 
    } 

    public string BtAddress 
    { 
     get { return btAddress; } 
     set { btAddress = string.IsNullOrWhiteSpace(value) ? null : value.Replace(":", "").Trim(); } 
    } 
    public string Name 
    { 
     get { return name; } 
     set { name = string.IsNullOrWhiteSpace(value) ? null : value.Trim(); } 
    } 
    public string Number 
    { 
     get { return number; } 
     set { number = string.IsNullOrWhiteSpace(value) ? null : value.Trim(); } 
    } 
    public bool IsValid 
    { 
     get { return isValid; } 
    } 
    public DateTime DateAdded 
    { 
     get { return dateAdded; } 
    } 
} 

一些方法被抑制

+0

你真的嘗試將兩個類序列化到一個文件,看看會發生什麼? – 2012-03-26 10:32:16

+1

你能提供A類和B類的代碼嗎? – ClayKaboom 2012-03-26 10:32:48

+0

@大衛:不,我沒有嘗試過。但是,它應該重要嗎?因爲webservice可以正確返回類型B. – waqaslam 2012-03-26 10:45:33

回答

2

爲了是可序列化的,一個類必須在其屬性上有公共setter。這就是A類和B類之間的區別,也是A不會序列化的原因。

大概:)

+0

我認爲你的意思是公共設置者*的屬性*,而不是*方法* – 2012-03-26 16:23:13

+0

我說過嗎?哦,親愛的 - 顯然沒有足夠的咖啡。歡呼@約翰桑德斯編輯它。 – 2012-03-26 22:02:16

0

我認爲問題可能與Customer類。也許它是私人或什麼的。嘗試檢查出來。