WCF服务类中的内部类和变量

时间:2012-03-15 18:40:17

标签: c# wcf

我在WCF上课了。在这个类中 - 有2个类,1个接口和几个变量。它添加了服务引用而没有错误。

[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,内部类及其方法和变量添加契约吗?

2 个答案:

答案 0 :(得分:3)

如果您希望它们在客户端上序列化且可见,则需要使用合同属性进行标记。

内部类不是一个好的做法,而是将所有操作放在一个标记为服务合同的接口中,然后将所有数据合同放在他们自己的类库中,这样您就可以引用客户端的那个组件。这有助于编写自己的代理和其他良好习惯。

答案 1 :(得分:0)

您的内部课程不会向您的服务的来电者公开,因为您的服务不会将它们用作参数或返回值。

相关问题