private Contact SQLGetContact(string query)
 {
     using (SqlConnection connection = new SqlConnection(config.connectionString))
     {
         Contact contact = new Contact();
         SqlCommand command = new SqlCommand(query, connection);
         connection.Open();
         SqlDataReader reader = command.ExecuteReader();
         try
         {
             if (reader.Read())
             {
                 // Loop through a list of contact fields that are configured and add them to the return object
                 foreach (string field in config.contactConfig.fields)
                 {
                     try
                     {
                         contact.GetType().GetProperty(field).SetValue(contact, reader[field].ToString(), null);
                     }
                     catch (Exception ex)
                     {
                         throw new WebFaultException<string>(String.Format("Column <{0}> not found in query results: {1}", field, ex.Message), HttpStatusCode.InternalServerError);
                     }
                 }
                 // Get all the phone numbers
                 contact.PhoneNumbers = getPhoneNumbers(reader, config.contactConfig.phoneCount, config.contactConfig.phoneMappings);
                 // Get all the email addresses
                 contact.EmailAddresses = getEmails(reader, config.contactConfig.emailCount, config.contactConfig.emailMappings);
                 if (config.contactConfig.getAddress)
                 {
                     // Get the address
                     contact.Address = getAddress(reader, config.contactConfig.addressFields);
                 }
             }
             else
             {
                 throw new WebFaultException<string>("No results found", HttpStatusCode.NoContent);
             }
         }
         catch (Exception ex)
         {
             throw ex;
         }
         finally
         {
             reader.Close();
         }
         return contact;
     }
 }
        public Contact(Contact clonedContact)
        {
            FirstName = clonedContact.FirstName;
            LastName = clonedContact.LastName;
            FullName = clonedContact.FullName;

            Id = clonedContact.Id;
            CustomAttribute = clonedContact.CustomAttribute;
            if (clonedContact.Address != null)
            {
                Address = new Address(clonedContact.Address);
            }
            if (clonedContact.EmailAddresses != null)
            {
                EmailAddresses = new EmailAddresses(clonedContact.EmailAddresses);
            }
            if (clonedContact.PhoneNumbers != null)
            {
                PhoneNumbers = new PhoneNumbers(clonedContact.PhoneNumbers);
            }
        }
        public ResponseContact GetContactByPhoneNumber(PhoneNumberRequest req)
        {
            string phoneNumber = req.PhoneNumber;
            ResponseContact rc = new ResponseContact();

            var ct = from contact in contacts where HasPhoneNumber(contact.PhoneNumbers.PhoneNumber, phoneNumber) select contact;
            if (ct.Count() < 1)
            {
                throw new WebFaultException(HttpStatusCode.NoContent);
            }
            else if (ct.Count() > 1)
            {
                throw new WebFaultException(HttpStatusCode.Conflict);
            }
            Contact retContact = new Contact(ct.FirstOrDefault());

            if ((retContact != null) && (req.CustomAttribute != null))
            {
                if (req.CustomAttribute.Equals("overwrite"))
                {
                    retContact.CustomAttribute = "overwritten custom attribute";
                }
                else if (req.CustomAttribute.Equals("error_never_return"))
                {
                    Thread.Sleep(System.Threading.Timeout.Infinite);
                }
                else if (req.CustomAttribute.Equals("error_internal_server"))
                {
                    throw new WebFaultException<string>("Thrown because error_internal_server was the custom attribute", HttpStatusCode.InternalServerError);
                }
            }

            rc.Contact = retContact;

            return rc;
        }