C# 如何在c wcf上正确使用重载构造函数
我正在学习一个教程,并试图以json格式传递一些数据。 我试图使构造函数过载,认为它将显示不同的数据,但事实并非如此 在这两种情况下,我得到相同的输出 这是我的界面:C# 如何在c wcf上正确使用重载构造函数,c#,wcf,C#,Wcf,我正在学习一个教程,并试图以json格式传递一些数据。 我试图使构造函数过载,认为它将显示不同的数据,但事实并非如此 在这两种情况下,我得到相同的输出 这是我的界面: namespace JsonWcfService { [ServiceContract] public interface IGetJson { // display user`s department [OperationContract] [WebInv
namespace JsonWcfService
{
[ServiceContract]
public interface IGetJson
{
// display user`s department
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/userDepartment/{name}")]
List<Departments> userDepartments(string name);
// display user`s app
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/userApp/{name}")]
List<Departments> userApp(string name);
}
}
这是我的班级:
namespace JsonWcfService
{
public class GetJson : IGetJson
{
//display user`s departments
public List<Departments> userDepartments(string name)
{
List<Departments> listUserDepartments = new List<Departments>();
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["tabletConnection"].ToString()))
{
conn.Open();
string cmdStr = String.Format("SELECT users.userName, users.departmentID, department.departmentName, users.isActive FROM users,department "
+ "WHERE users.departmentID = department.departmentID "
+ "AND userName = '" + name +"'");
SqlCommand cmd = new SqlCommand(cmdStr, conn);
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
while (rd.Read())
listUserDepartments.Add(new Departments(rd.GetString(0), rd.GetInt32(1), rd.GetString(2), rd.GetString(3)));
}
conn.Close();
}
return listUserDepartments;
}
//display user`s app
public List<Departments> userApp(string name)
{
List<Departments> listUserApp = new List<Departments>();
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["tabletConnection"].ToString()))
{
conn.Open();
string cmdStr = String.Format("SELECT * FROM application");
SqlCommand cmd = new SqlCommand(cmdStr, conn);
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
while (rd.Read())
listUserApp.Add(new Departments(rd.GetInt32(0), rd.GetString(1)));
}
conn.Close();
}
return listUserApp;
}
}
[DataContract]
public class Departments
{
[DataMember]
public int departmentId { get; set; }
[DataMember]
public string departmentName { get; set; }
[DataMember]
public string userName { get; set; }
[DataMember]
public string isActive { get; set; }
public Departments(int temp_departmentId, string temp_departmentName)
{
departmentId = temp_departmentId;
departmentName = temp_departmentName;
}
public Departments(string temp_userName, int temp_departmentId, string temp_departmentName, string temp_isActive)
{
userName = temp_userName;
departmentId = temp_departmentId;
departmentName = temp_departmentName;
isActive = temp_isActive;
}
}
}
构造函数1的输出:
{userAppResult:[{departmentId:1,departmentName:A,isActive:null,userName:null},{departmentId:2,departmentName:A,isActive:null,userName:null},{departmentId:3,departmentName:A,isActive:null,userName:null},{departmentId:4,departmentName:A,isActive:null,userName:null},{departmentId:5,departmentName:A,isActive:null}]
构造函数2的输出:
{userDepartmentsResult:[{departmentId:1,departmentName:A,isActive:Y,userName:b},{departmentId:2,departmentName:A,isActive:Y,userName:b},{departmentId:3,departmentName:A,isActive:Y,userName:b}这与重载无关。您试图让WCF不序列化没有值集的属性。序列化程序不知道您调用了哪个构造函数,因此它只会序列化整个对象,包括标记为DataMember的所有属性 有几种方法可以做到这一点。可以将DataMember属性的EmitDefaultValue设置为false以忽略具有默认值的属性,如和中所述 您也可以使用
还要注意的是,如果这是为了学习,我建议研究ASP.NETWebAPI而不是WCFREST。您还应该使用,而不是使用用户提供的变量通过字符串连接手工制作SQL 您希望获得什么样的输出?放弃本教程。如果要构建REST服务,请使用ASP.NET WebAPI。您对内联SQL的使用也使您的服务容易受到SQL注入攻击。我已经更新了问题,添加了输出I getWell,当您执行选择时,选择返回什么?构造函数1 userAppResult不应该只返回departmentId和departmentName吗?这就是我正在努力实现的目标。