我需要在WCF服务中使用一个类才能返回公共DataMembers。我能这样做吗?原因:我正在尝试编写新服务来替换现有服务,并通过仅更改Web配置中的端点(而不是重新部署)将现有应用程序指向新服务。 为了匹配旧服务中的对象,我需要这个新类只返回公共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
}
}
我怎样才能只返回公众成员,为什么我不在做什么呢? (它有效,我得到数据,它返回到调用应用程序的对象与应用程序的预期不匹配)。 感谢