2012-03-15 35 views
0

我有WCF類。在這個類裏面 - 有2個類,1個接口和幾個變量。它作爲服務參考添加,沒有錯誤。WCF服務類中的內部類和變量

[ServiceContract(Namespace = "")] 
[SilverlightFaultBehavior] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class XmlService 
{ 
    private string filename; 
    private XmlTextReader xmlreader; 
    List<IWeather> returner=new List<IWeather>(); 
    [OperationContract] 
    public void DoWork() 
    { 
    // Add your operation implementation here 
    return; 
    } 

    interface IWeather 
    { 
     string GetCondition { get; set; } 
     DateTime GetDate { get; set; } 
    } 

    public class Current_Weather:IWeather 
    { 
     private string condition, humidity, wind_condition; 
     private int temp_f, temp_c; 
     private DateTime day; 
     public string GetCondition 
     { 
      get { return condition; } 
      set { condition = value; } 
     } 
     public string GetHumidity 
     { 
      get { return humidity; } 
      set { humidity = value; } 
     } 
     public string GetWindCondition 
     { 
      get { return wind_condition; } 
      set { wind_condition = value; } 
     } 
     public int TEMP_F 
     { 
      get { return temp_f; } 
      set { temp_f = value; } 
     } 
     public int TEMP_C 
     { 
      get { return temp_c; } 
      set { temp_c = value; } 
     } 
     public DateTime GetDate 
     { 
     get { return day; } 
     set { day = value; } 
     } 
    } 
    public class Forecast_Weather:IWeather 
    { 
     public string condition; 
     public int lowT, highT; 
     public DateTime day; 
     public string GetCondition 
     { 
      get { return condition; } 
      set { condition = value; } 
     } 
     public int GetLowT 
     { 
      get { return lowT; } 
      set { lowT = value; } 
     } 
     public int HighT 
     { 
      get { return highT; } 
      set { highT = value; } 
     } 
     public DateTime GetDate 
     { 
      get { return day; } 
      set { day = value; } 
     } 
    } 


} 

我應該補充合同變量,接口IWeather,對於內部類和它的方法和變量?

回答

3

如果你想讓它們在客戶端上被序列化並且可見,他們需要用契約屬性進行標記。

內部類並不是一個好的做法,而是將所有操作放在一個標記爲服務契約的界面中,然後將所有數據契約放置在它們自己的類庫中,以便您可以引用您的客戶端。這有助於編寫自己的代理和其他良好習慣。

+1

也是,避免枚舉 – deltree 2012-03-15 18:50:34

+0

我已經將它添加到我的silverlight項目中作爲啓用silverlight的wcf服務,並且它沒有生成帶有接口聲明的文件。 – 2012-03-15 18:51:29

0

您的內部類將不會公開給您的服務調用者,因爲您的服務不會將它們用作參數或返回值。