如何在c#wcf上正确使用重载构造函数

时间:2015-05-05 10:15:00

标签: c# wcf

我正在按照教程并尝试以json格式传递一些数据。 我试图重载构造函数,认为它会显示不同的数据,但事实并非如此。

在这两种情况下,我得到相同的输出。

这是我的界面:

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, “username” 的日期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”}]}

1 个答案:

答案 0 :(得分:0)

这与重载无关。您试图让WCF不序列化没有设置值的属性。序列化程序不知道您调用了哪个构造函数,因此它只是序列化整个对象,包括标记为DataMember的所有属性。

有几种方法可以做到这一点。您可以将EmitDefaultValue属性的DataMember设置为false,以忽略具有默认值的属性,如how to not return null when a Data member field is not set in the data contractHow to remove null DataMember properties from the response in wcf中所述。

您也可以使用ShouldSerialize convention

另请注意my comment,当这是用于学习目的时,我建议查看ASP.NET WebAPI而不是WCF REST。您还应该使用parameterized queries而不是通过字符串连接手动制作SQL与用户提供的变量。