简体   繁体   中英

Consuming wcf service Windows Form Application

I am consuming Wcf Service in Windows Form Application . I am trying to create user login system based on user emu type from sql database .When I enter the value 1 into textbox it should return full time employee method else value 2 into textbox it should return part time employee method but Its is not working according expectation ..

Here is Employee class code ....

    [KnownType(typeof(FullTimeEmployee))]
    [KnownType(typeof(PartTimeEmployee))]
    [DataContract(Namespace = "http://pragimtech.com/Employee")]
    public class Employee
    {
        private int _id;
        private string _name;
        private string _gender;
        private DateTime _dateOfBirth;

        [DataMember(Order = 1)]
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        [DataMember(Order = 2)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        [DataMember(Order = 3)]
        public string Gender
        {
            get { return _gender; }
            set { _gender = value; }
        }

        [DataMember(Order = 4)]
        public DateTime DateOfBirth
        {
            get { return _dateOfBirth; }
            set { _dateOfBirth = value; }
        }

        [DataMember(Order = 5)]
        public EmployeeType Type { get; set; }
    }
    [DataContract(Name = "EmployeeType")]
    public enum EmployeeType
    {
        [EnumMember]
        FullTimeEmployee = 1,
        [EnumMember]
        PartTimeEmployee = 2
    }
}

Here is my Full time and part time employee class inherit from Employee class...

     public class FullTimeEmployee : Employee
        {
            public int AnnualSalary { get; set; }
        }
      public class PartTimeEmployee : Employee
        {
            public int HourlyPay { get; set; }
            public int HoursWorked { get; set; }
        }

Here is Method to Get Employee method to access employee based on employee type...

 public Employee GetEmployee(int Id)
        {
            Employee employee = null;
            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("spGetEmployee1", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter parameterId = new SqlParameter();
                parameterId.ParameterName = "@EmployeeType";
                parameterId.Value = Id;
                cmd.Parameters.Add(parameterId);
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    if ((EmployeeType)reader["EmployeeType"] == EmployeeType.FullTimeEmployee)
                    {
                        return employee;
                       }                    }
                    else if ((EmployeeType)reader["EmployeeType"] == EmployeeType.PartTimeEmployee)
                    {

                        return employee;
                       }
                }
            }
            return employee;
        }

Here is my Windows Form Application ......

  private void button1_Click(object sender, EventArgs e)
        {
            MyService.HalifaxServiceClient myservice = new MyService.HalifaxServiceClient("NetTcpBinding_IHalifaxService");
            MyService.Employee employee = myservice.GetEmployee(Convert.ToInt32(txt1.Text));
            MyService.FullTimeEmployee ft = new MyService.FullTimeEmployee();

            if (employee == myservice.GetEmployee(Convert.ToInt32(txt1.Text).CompareTo(employee.Type)))
            {
                FulltimeEmployeeLinkActivites();
            }
            else if (employee == myservice.GetEmployee(Convert.ToInt32(txt1.Text).CompareTo(employee.Type)))
            {

                PartTimeEmployeeActivities();
            }
            else
            {
                label4.Text = "No infomation found";
            }


        }

Here is screen shot when I run the application ... 单击此处查看应用程序

The problem I see with your if / else is that the conditional statements are exactly the same. One way that you can branch based on the type of an object is with the is keyword.

if (employee is FullTimeEmployee) 
{
    FulltimeEmployeeLinkActivites();
}
else if (employee is PartTimeEmployee)
{
    PartTimeEmployeeActivities();
}
else
{
    label4.Text = "No information found";
}

I would also add that this is not necessarily best practice, however it should get you what you are asking for.

In addition to that, your method that returns the employee instance never returns an employee of a valid type. It doesn't appear that the GetEmployee method ever instantiates an employee instance. It looks like it is always returning null. Try returning instances of the proper type. You will also need to populate the instances with the data you need.

if ((EmployeeType)reader["EmployeeType"] == EmployeeType.FullTimeEmployee)
{
    return new FullTimeEmployee();
}
else if ((EmployeeType)reader["EmployeeType"] == EmployeeType.PartTimeEmployee)
{
    return new PartTimeEmployee();
} 

You have a confusing naming in your elements and "employee" class does not seems to be populated in your data reader.

"FullTimeEmployee" is the name one of your classes and also the name of one of your enums. So is not safe to set your condition

(EmployeeType)reader["EmployeeType"] == EmployeeType.FullTimeEmployee

I can not be sure without the code of your spGetEmployee, but if it returns values from your table with those same names it could be safer to declare

while(reader.Read())
{
    employee= new employee();
    employee.Id= reader.GetInt32(0);
    employee.Name= reader.GetString(1);
    ...
    employee.EmployeeType=(EmployeeType)reader.GetInt32(4);

    if(employee.EmployeeType== EmployeeType.FullTimeEmployee)
    {
        //Do extra work for this type of employee
        ...
        return employee;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM