0

パブリック DataMembers のみを返すために、WCF サービスにクラスが必要です。それをしてもいいですか?理由: 既存のサービスを置き換える新しいサービスを作成しようとしています。また、Web 構成のみ (再デプロイではなく) でエンドポイントを変更することにより、既存のアプリケーションを新しいサービスに向けようとしています。古いサービスのオブジェクトと一致させるには、この新しいクラスが public DataMember のみを返す必要があります。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Data.Objects;
using PPS.Services.Location.Data;
using PPS.Services.Location.Business;
using log4net;

    namespace PPS.Services.Location
    {
        /// <summary>
        /// For applications that were using the old service.
        /// </summary>
        [DataContract]
        [KnownType(typeof(SchoolLocation))]
        public class SchoolLocation
        {
            protected static readonly ILog logger = LogManager.GetLogger(typeof(SchoolLocation));

            #region Properties        
            [DataMember]
            public string Name { get; set; }
            [DataMember]
            public string StateID { get; set; }
            [DataMember]
            public string HRMS_ID { get; set; }
            [DataMember]
            public string Street { get; set; }
            [DataMember]
            public string State { get; set; }
            [DataMember]
            public string City { get; set; }
            [DataMember]
            public string Zip { get; set; }


            private string CurrentYear
            {
                get
                {
                    return SchoolYear.Current.ToString();
                }
            }

            internal protected List<SchoolLocation> OpenSchools { get; set; }

            internal protected LocationStyle LocationStyle { get; set; }

            internal protected string HighSchoolStyle { get { return "3"; } }
            internal protected string MiddleSchoolStyle { get { return "2"; } }
            internal protected string ElemSchoolStyle { get { return "1"; } }
            internal protected List<string> elemMidHighSchools = new List<string>();
            internal protected List<string> ElemMidHighSchools
            {
                get
                {
                    elemMidHighSchools.Add(HighSchoolStyle);
                    elemMidHighSchools.Add(MiddleSchoolStyle);
                    elemMidHighSchools.Add(ElemSchoolStyle);
                    return this.elemMidHighSchools;
                }
            }
            internal protected List<string> elemMidHighAltSchools = new List<string>();
            internal protected List<string> ElemMidHighAltSchools
            {
                get
                {
                    elemMidHighSchools.Add(HighSchoolStyle);
                    elemMidHighSchools.Add(MiddleSchoolStyle);
                    elemMidHighSchools.Add(ElemSchoolStyle);
                    elemMidHighSchools.Add(AltSchoolStyle);
                    return this.elemMidHighSchools;
                }
            }
            internal protected List<string> allSchools = new List<string>();
            internal protected List<string> AllSchools
            {
                get
                {
                    allSchools.Add(HighSchoolStyle);
                    allSchools.Add(MiddleSchoolStyle);
                    allSchools.Add(ElemSchoolStyle);
                    allSchools.Add(AltSchoolStyle);
                    allSchools.Add(CBOSchoolStyle);
                    allSchools.Add(CharterSchoolStyle);
                    allSchools.Add(HeadstartSchoolStyle);
                    return allSchools;
                }
            }
            internal protected string AltSchoolStyle { get { return "4"; } }
            internal protected string CBOSchoolStyle { get { return "5"; } }
            internal protected string DartSchoolStyle { get { return "6"; } }
            internal protected string CharterSchoolStyle { get { return "7"; } }
            internal protected string SpecEdSchoolStyle { get { return "8"; } }
            internal protected string AdministrativeStyle { get { return "20"; } }
            internal protected string HeadstartSchoolStyle { get { return "15"; } }
            #endregion

            #region Constructor
            public SchoolLocation()
            {
                OpenSchools = new List<SchoolLocation>();
            }
            #endregion

            #region Methods
            public void GetOpenSchools(LocationStyle style)
            {
                try
                {
                    using (PPSLocationService db = new PPSLocationService())
                    {
                        string LocationYear = CurrentYear;
                        //if the current years locations have not been added yet, then use last years location.
                        //only do this once though, if last year does not work then return null.
                        //if (GetLocationCount(LocationYear, style) == 0) //allow for some rogues
                        //{
                        //    LocationYear = SchoolYear.LastYear.ToString();
                        //}
                        var query = (from ld in db.PPSOpenLocations
                                     where ld.SchoolYear == LocationYear
                                     select ld);

                        switch (style)
                        {
                            case LocationStyle.AllElemMidHighSchools:
                                {
                                    query = query.Where(x => ElemMidHighSchools.Contains(x.LocationStyle));
                                    break;
                                }
                            case LocationStyle.AllElemNidHighAltSchools:
                                {
                                    query = query.Where(x => ElemMidHighAltSchools.Contains(x.LocationStyle));
                                    break;
                                }
                            case LocationStyle.AllSchools:
                                {
                                    query = query.Where(x => AllSchools.Contains(x.LocationStyle));
                                    break;
                                }
                            case LocationStyle.Elementary:
                                {
                                    query = query.Where(x => x.LocationStyle == ElemSchoolStyle);
                                    break;
                                }
                            case LocationStyle.Middle:
                                {
                                    query = query.Where(x => x.LocationStyle == MiddleSchoolStyle);
                                    break;
                                }
                            case LocationStyle.High:
                                {
                                    query = query.Where(x => x.LocationStyle == HighSchoolStyle);
                                    break;
                                }
                            case LocationStyle.Alternative:
                                {
                                    query = query.Where(x => x.LocationStyle == AltSchoolStyle);
                                    break;
                                }
                            case LocationStyle.CBO:
                                {
                                    query = query.Where(x => x.LocationStyle == CBOSchoolStyle);
                                    break;
                                }
                            case LocationStyle.Dart:
                                {
                                    query = query.Where(x => x.LocationStyle == DartSchoolStyle);
                                    break;
                                }
                            case LocationStyle.Charter:
                                {
                                    query = query.Where(x => x.LocationStyle == CharterSchoolStyle);
                                    break;
                                }
                            case LocationStyle.SpecEd:
                                {
                                    query = query.Where(x => x.LocationStyle == SpecEdSchoolStyle);
                                    break;
                                }
                            case LocationStyle.HeadStart:
                                {
                                    query = query.Where(x => x.LocationStyle == HeadstartSchoolStyle);
                                    break;
                                }
                            default:
                                {
                                    break;
                                }
                        }

                        OpenSchools = SchoolLocation.FromDbEntities(from ld in query select ld).ToList();
                    }
                }
                catch (Exception ex)
                {
                    logger.Error("GetOpenLocations", ex);
                    throw;
                } 
            }

            public void GetSchoolLocationsByName(string PartialName)
            {
                using (PPSLocationService db = new PPSLocationService())
                {
                    var query = (from ld in db.PPSOpenLocations
                                 where ld.SchoolYear == CurrentYear
                                 && ElemMidHighAltSchools.Contains(ld.LocationStyle)
                                 && PartialName.Length > 0
                                 && ld.LocationName.ToLower().Contains(PartialName)
                                 select ld);

                    OpenSchools = SchoolLocation.FromDbEntities(from ld in query select ld).ToList();
                }
            }

            public void GetSchoolLocationByHRMSId(string Id)
            {
                using (PPSLocationService db = new PPSLocationService())
                {
                    var query = (from ld in db.PPSOpenLocations
                                 where ld.SchoolYear == CurrentYear
                                 && ElemMidHighAltSchools.Contains(ld.LocationStyle)
                                 && ld.HRMS_ID == Id
                                 select ld);

                    OpenSchools = SchoolLocation.FromDbEntities(from ld in query select ld).ToList();
                }
            }

            public void GetSchoolLocationByStateId(string Id)
            {
                SchoolLocation schoolLocation = new SchoolLocation();
                using (PPSLocationService db = new PPSLocationService())
                {
                    //schoolLocation = SchoolLocation.FromDbEntity((from ld in db.PPSOpenLocations
                    //                                              where ld.SchoolYear == CurrentYear
                    //                                              && ElemMidHighAltSchools.Contains(ld.LocationStyle)
                    //                                              && ld.StateID == Id
                    //                                              select ld).FirstOrDefault());
                    var query = (from ld in db.PPSOpenLocations
                                 where ld.SchoolYear == CurrentYear
                                 && ElemMidHighAltSchools.Contains(ld.LocationStyle)
                                 && ld.StateID == Id
                                 select ld);

                    OpenSchools = SchoolLocation.FromDbEntities(from ld in query select ld).ToList();
                }
            }
            #endregion

            #region Static Methods
            public static SchoolLocation FromDbEntity(PPSOpenLocation dbLocation)
            {
                if (dbLocation != null)
                {
                    SchoolLocation sch = new SchoolLocation();

                    sch.Name = dbLocation.LocationName;
                    sch.HRMS_ID = dbLocation.HRMS_ID;
                    sch.StateID = dbLocation.StateID;
                    sch.Street = dbLocation.Address;
                    sch.City = dbLocation.City;
                    sch.State = dbLocation.State;
                    sch.Zip = dbLocation.PostalCode;

                    return sch;                
                }
                return null;
            }

            public static IEnumerable<SchoolLocation> FromDbEntities(IEnumerable<PPSOpenLocation> dbLocations)
            {
                if (dbLocations != null)
                {
                    foreach (PPSOpenLocation dbLocation in dbLocations)
                    {
                        yield return FromDbEntity(dbLocation);
                    }
                }
                yield break;
            }
            #endregion
        }
    }

パブリック メンバーのみを返すにはどうすればよいですか? (動作します。データを取得します。呼び出し元のアプリに返されるオブジェクトが、アプリが期待しているものと一致しません)。ありがとう

4

1 に答える 1

0

おそらくIgnoreDataMemberAttributeを探していますか? 抑制したいプロパティの上に配置するとうまくいくはずです。

于 2013-10-04T21:39:53.617 に答える