0

I am following a tutorial and trying to pass some data in json format. I have tried to overload the constructor thinking it will display different data but this is not the case.

On both cases i get the same output.

this is my interface:

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);       

    }
}

this is my 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;
        }
    }
}

output for constructor 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}]}

output for constructor 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"}]}

eMRe
  • 3,097
  • 5
  • 34
  • 51
  • 1
    What output are you looking to have? – Coder1409 May 05 '15 at 10:24
  • 1
    Ditch this tutorial. [WCF REST services are old and no longer maintained](https://msdn.microsoft.com/en-us/library/jj823172%28v=vs.110%29.aspx). If you want to build a REST service, use ASP.NET WebAPI. Your use of inline SQL also makes your service susceptible to SQL injection attacks. – CodeCaster May 05 '15 at 10:38
  • I have updated the question, added the output i get – eMRe May 05 '15 at 10:41
  • Well, what do the selects return when you execute them? – Thorsten Dittmar May 05 '15 at 10:48
  • Shouldn`t constructor 1 (userAppResult) return only departmentId and departmentName? That is what i am trying to achieve. – eMRe May 05 '15 at 10:51

1 Answers1

0

This has nothing to do with overloading. You're trying to let WCF not serialize properties that don't have a value set. The serializer doesn't know which constructor you called, so it's just going to serialize the entire object, including all properties marked as DataMember.

There are a couple ways to do so. You can set the EmitDefaultValue of the DataMember attribute to false to ignore properties that have a default value, as explained in 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.

You could also use the ShouldSerialize convention.

Also note my comment, 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 instead of manually crafting SQL by string concatenation with user-provided variables.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272