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

How to use overloading Constructor properly on 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);       

    }
}

这是我的 class:

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":空}]}

构造函数 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 以忽略具有默认值的属性,如 how to not return null when a Data member field is not set in the data contract and How to remove null DataMember properties from the response in wcf 中所述。

您也可以使用 ShouldSerialize convention

另请注意 , when this is for learning purposes I'd suggest looking into ASP.NET WebAPI as opposed to WCF REST. You also should use parameterized queries 而不是通过将字符串与用户提供的变量连接来手动制作 SQL。