none
Passing generics between DAL and WCF RRS feed

  • Question

  • Hi,

    I have a solution with layers including wcf. UI calls the WCF.

    I am getting an error when calling a List in Service1.svc from the DAL layer. How can I fix it?

    Thanks

     var client = new ServiceReference1.PizzaAPIClient();
                var customer = new ServiceReference1.CustomerAPI();
                 
                 customer.PhoneNumber = txtPhone.text;
                    List<ServiceReference1.CustomerAPI>  lstcustomers  = client.GetCustomerByPhoneNumber(customer);
                     

    WCF

    IService1.cs

    using BL;
    
    namespace WcfService1
    {
         
        [ServiceContract]
        public interface IPizzaAPI
        {
            
            // TODO: Add your service operations here
            [OperationContract]
            List<CustomerAPI> GetCustomerByPhoneNumber(CustomerAPI customer);
           
        }
    
        [DataContract]
        public class OrderAPI  
        {
    
        
            [DataMember]
            public string OrderName { get; set; }
    
            }
    
        
         [DataContract]
        public class CustomerAPI
        {
                int _Customerid;
                string _firstName;
                string _lastName;
                 string _phonenumber;
             public CustomerAPI (int custId, string firstname, string lastName, string phonenumber)
            {
                _Customerid = custId;
                _lastName = lastName;
                _firstName = firstname;
                  _phonenumber = phonenumber;
            }
    
            
             [DataMember]
                 public string PhoneNumber
                 {
                     get { return _phonenumber; }
                     set { _phonenumber = value; }
                 }
             [DataMember]
                 public int CustomerId
                 {
                     get { return _Customerid; }
                     set { _Customerid = value; }
                 }
             --omitted for brevity
        }

    Service1.svc

    using DL; using BL; namespace WcfService1 { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. public class Service1 : IPizzaAPI { List<CustomerAPI> customerLst = new List<CustomerAPI>(); // Method returning the details of the student for the studentId public List<CustomerAPI> GetCustomerByPhoneNumber(CustomerAPI customer) { List<CustomerAPI> customerLst = DL.CustomerDB.GetCustomerByPhoneNumber(customer.PhoneNumber);

    //Error 8 Cannot implicitly convert type 'System.Collections.Generic.List<BL.CustomerBL>' to 'System.Collections.Generic.List<WcfService1.CustomerAPI>' return customerLst; } }

    CustomerBL.cs -Business Logic Layer

    namespace BL
    {
       public  class CustomerBL
        {
    
            private string phonenumber;
            public int CustomerId { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            
    
            public String PhoneNumber
            {
                get
                {
                    return this.phonenumber;
                }
                set
                {
                    this.phonenumber = value;
                    if (this.PhoneNumber == "")
                    {
                        throw new Exception("Please provide Tel ...");
                    }
    
                }
            }
        }
    }

    CustomerDB.cs -DAL layer

    using BL;
    namespace DL
    {
        public static class CustomerDB
        {
    
            public static SqlConnection InitConnection()
            {
                DL.ConStrings connstrings = new DL.ConStrings();
                return connstrings.GetPizzaDeliveryConnection();
    
            }
            //public static CustomerBL[] GetCustomerByPhoneNumber(string phonenumber)
            public static List<CustomerBL> GetCustomerByPhoneNumber(string phonenumber)
            {
                SqlConnection connstrings = InitConnection();
                using (var connection = connstrings)//using doesn't require connection closed
                {
                    var cmd = connection.CreateCommand();
                    cmd.CommandText = "GetCustomerByPhoneNumber";
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@phonenumber", phonenumber);
                    connection.Open();
                    var customers = new List<CustomerBL>();
    
                    var reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        var customer = new CustomerBL();
                        customer.CustomerId = Convert.ToInt32(reader["CustomerId"]);
                        customer.FirstName = Convert.ToString(reader["FirstName"]);
                        customer.LastName = Convert.ToString(reader["LastName"]);
                        customer.Address = Convert.ToString(reader["Address"]);
    
                        customers.Add(customer);
                    }
    
                    return customers;
                }
            }
    Thursday, February 5, 2015 8:18 PM

Answers

  • Hi collie12,

    >>Cannot implicitly convert type 'System.Collections.Generic.List<BL.CustomerBL>' to 'System.Collections.Generic.List<WcfService1.CustomerAPI>'   

    As the error said that we can not simply convert the 'System.Collections.Generic.List<BL.CustomerBL>' to 'System.Collections.Generic.List<WcfService1.CustomerAPI>' by using the following code, because they are different classes:

    List<CustomerAPI> customerLst = DL.CustomerDB.GetCustomerByPhoneNumber(customer.PhoneNumber); 


    Please try to modify the above code as following:

     public List<CustomerAPI> GetCustomerByPhoneNumber(CustomerAPI customer)
            {
                List<BL.CustomerBL> CustomerBLList= DL.CustomerDB.GetCustomerByPhoneNumber(customer.PhoneNumber);
                List<CustomerAPI> CustomerAPIList = new List<CustomerAPI>();
                foreach (var CustomerBL in CustomerBLList)
                {
                    CustomerAPI CustomerAPI = new CustomerAPI(CustomerBL.CustomerId, CustomerBL.FirstName, CustomerBL.LastName, CustomerBL.PhoneNumber);
                    CustomerAPIList.Add(CustomerAPI);
                }
               return CustomerAPIList;       
            

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, February 6, 2015 6:29 AM
    Moderator

All replies

  • Hi collie12, The reason you are getting this error is because at Line 8 we are having a type mismatch. So solve this problem you can declare the CustomerAPI DataContract in the class library from from your DAL delete the CustomerBL class. Also in your GetCustomerByPhoneNumber() method return a list object of type CustomerAPI datacontract. Hope this makes sense,let me know if you face any issues.
    Friday, February 6, 2015 5:59 AM
  • Hi collie12,

    >>Cannot implicitly convert type 'System.Collections.Generic.List<BL.CustomerBL>' to 'System.Collections.Generic.List<WcfService1.CustomerAPI>'   

    As the error said that we can not simply convert the 'System.Collections.Generic.List<BL.CustomerBL>' to 'System.Collections.Generic.List<WcfService1.CustomerAPI>' by using the following code, because they are different classes:

    List<CustomerAPI> customerLst = DL.CustomerDB.GetCustomerByPhoneNumber(customer.PhoneNumber); 


    Please try to modify the above code as following:

     public List<CustomerAPI> GetCustomerByPhoneNumber(CustomerAPI customer)
            {
                List<BL.CustomerBL> CustomerBLList= DL.CustomerDB.GetCustomerByPhoneNumber(customer.PhoneNumber);
                List<CustomerAPI> CustomerAPIList = new List<CustomerAPI>();
                foreach (var CustomerBL in CustomerBLList)
                {
                    CustomerAPI CustomerAPI = new CustomerAPI(CustomerBL.CustomerId, CustomerBL.FirstName, CustomerBL.LastName, CustomerBL.PhoneNumber);
                    CustomerAPIList.Add(CustomerAPI);
                }
               return CustomerAPIList;       
            

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, February 6, 2015 6:29 AM
    Moderator
  • Not sure I understood fully. Is what you are suggesting the same as Amy's solution?

    Friday, February 6, 2015 9:21 AM
  • Yes exactly!
    Saturday, February 7, 2015 3:57 AM
  • Thanks all. It is working :)
    Saturday, February 7, 2015 8:25 PM