public ServiceRequest UpdateSR(ServiceRequest sr, int _logIncidentId = 0, int _logContactId = 0)
        {
            if (String.IsNullOrWhiteSpace(SRURL) || String.IsNullOrWhiteSpace(SRServiceUsername) || String.IsNullOrWhiteSpace(SRServicePassword))
            {
                throw new Exception("Provider's InitForSR not run.");
            }

            string request, response, logMessage, logNote;

            SRSVC.WC_Service_Request_BSClient client = new SRSVC.WC_Service_Request_BSClient(binding, addr);
            MyEndpointBehavior eBehavior = new MyEndpointBehavior();
            client.Endpoint.Behaviors.Add(eBehavior);
            if(SRServiceTimeout > 0)
                client.InnerChannel.OperationTimeout = TimeSpan.FromMilliseconds(SRServiceTimeout );

            SRSVC.WC_Service_Request_BSUpdate_Input ip = new SRSVC.WC_Service_Request_BSUpdate_Input();


            ip.ListOfWc_Service_Request_Io = new SRSVC.ListOfWc_Service_Request_IoData();
            ip.ListOfWc_Service_Request_Io.ServiceRequest = new SRSVC.ServiceRequestData[1];
            ip.ListOfWc_Service_Request_Io.ServiceRequest[0] = new SRSVC.ServiceRequestData();
            if (sr.RequestID != null)
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].Id = sr.RequestID;
            }
            else
            {
                sr.ErrorMessage = "The following error occurred when doing the Create: SR ID is empty";
                return sr;
            }


            if (sr.Summary != null)
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].Description = sr.Summary;
            }
            else
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].Description = String.Empty;
            }

            if (sr.ContactID != null)
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].ContactId = sr.ContactID;
            }
            else
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].ContactId = String.Empty;
            }

            if (sr.Severity != null)
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].Severity = sr.Severity;
            }
            else
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].Severity = String.Empty;
            }

            if (sr.RequestType != null)
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].SRType = sr.RequestType;
            }
            else
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].SRType = String.Empty;
            }

            if (sr.SerialNumber != null)
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].SerialNumber = sr.SerialNumber;
            }
            else
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].SerialNumber = String.Empty;
            }

            if (sr.Status != null)
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].Status = sr.Status;
            }
            else
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].Status = String.Empty;
            }

            if (sr.IncidentID != null || sr.IncidentRef != null)
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].IntegrationId = sr.IncidentID + "," + sr.IncidentRef;
            }

            if (sr.RnowHost != null)
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].IntegrationSite = sr.RnowHost;
            }
            else
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].IntegrationSite = String.Empty;
            }

            ip.LOVLanguageMode = "LIC";
            ip.ViewMode = "All";
            Stopwatch stopwatch = new Stopwatch();
            try
            {
                SRSVC.WC_Service_Request_BSUpdate_Output op;
                using (new OperationContextScope(client.InnerChannel))
                {
                    MessageHeader usrMsgHdr = MessageHeader.CreateHeader("UsernameToken", "http://siebel.com/webservices", SRServiceUsername);
                    OperationContext.Current.OutgoingMessageHeaders.Add(usrMsgHdr);
                    MessageHeader pwdMsgHdr = MessageHeader.CreateHeader("PasswordText", "http://siebel.com/webservices", SRServicePassword);
                    OperationContext.Current.OutgoingMessageHeaders.Add(pwdMsgHdr);
                    stopwatch.Start();
                    op = client.WC_Service_Request_BSUpdate(ip);
                    stopwatch.Stop();
                    request = eBehavior.msgInspector.reqPayload;
                    response = eBehavior.msgInspector.resPayload;

                    SRSVC.ServiceRequestId opData = op.ListOfWc_Service_Request_Io[0];
                    sr.RequestID = opData.Id;
                }

                logMessage = "Request of updating Service Request (Success). SR ID = " + sr.RequestID;
                logNote = "Request Payload: " + request;
                log.DebugLog(_logIncidentId, _logContactId, logMessage, logNote);

                logMessage = "Response of updating Service Request (Success). SR ID = " + sr.RequestID;
                logNote = "Response Payload: " + response;
                log.DebugLog(_logIncidentId, _logContactId, logMessage, logNote, (int)stopwatch.ElapsedMilliseconds);
            }
            catch (Exception ex)
            {
                request = eBehavior.msgInspector.reqPayload;
                response = eBehavior.msgInspector.resPayload;
                sr.ErrorMessage = "There has been an error communicating with Siebel. Please check log for detail.";

                logMessage = "Request of updating Service Request (Failure). SR ID = " + sr.RequestID + " Error: " + ex.Message;
                logNote = "Request Payload: " + request;
                log.ErrorLog(_logIncidentId, _logContactId, logMessage, logNote);

                logMessage = "Response of updating Service Request (Failure). SR ID = " + sr.RequestID + " Error: " + ex.Message;
                logNote = "Response Payload: " + response;
                log.ErrorLog(_logIncidentId, _logContactId, logMessage, logNote);

                handleSiebelException(ex, "Update Service Request", _logIncidentId, _logContactId);
            }
            return sr;
        }
        /*  call WC_Service_Request_BSClient : WC_Service_Request_BSQueryPage
         *  Output : WC_Service_Request_BSQueryPage_Output.ListOfWc_Service_Request_Io.ServiceRequest
         *  call dictAddProperty() to add the individual property name, type, and value
         *  for dynamic columns feature
         */
        public Dictionary<string, string> LookupSRDetail(IList<string> columns, string srId, int _logIncidentId = 0, int _logContactId = 0)
        {
            // can reuse Contact URL, service username and password, is all the same 
            if (String.IsNullOrWhiteSpace(SRURL) || String.IsNullOrWhiteSpace(SRServiceUsername) || String.IsNullOrWhiteSpace(SRServicePassword))
            {
                throw new Exception("Provider's InitForContact not run.");
            }
            string request, response, logMessage, logNote;
            SRSVC.WC_Service_Request_BSClient client = new SRSVC.WC_Service_Request_BSClient(binding, addr);
            MyEndpointBehavior eBehavior = new MyEndpointBehavior();
            client.Endpoint.Behaviors.Add(eBehavior);
            if(SRServiceTimeout > 0)
                client.InnerChannel.OperationTimeout = TimeSpan.FromMilliseconds(SRServiceTimeout );
            SRSVC.WC_Service_Request_BSQueryPage_Input ip = new SRSVC.WC_Service_Request_BSQueryPage_Input();
            ip.ListOfWc_Service_Request_Io = new SRSVC.ListOfWc_Service_Request_IoQuery();
            ip.ListOfWc_Service_Request_Io.ServiceRequest = new SRSVC.ServiceRequestQuery();

            foreach (PropertyInfo propertyInfo in ip.ListOfWc_Service_Request_Io.ServiceRequest.GetType().GetProperties())
            {
                foreach (string column in columns)
                {
                    if (propertyInfo.Name == column.Split('.')[1])
                    {
                        if (propertyInfo.PropertyType == typeof(SRSVC.queryType))
                        {
                            SRSVC.queryType queryType = new SRSVC.queryType();
                            propertyInfo.SetValue(ip.ListOfWc_Service_Request_Io.ServiceRequest, queryType, null);
                        }
                        break;
                    }
                }
            }

            ip.ListOfWc_Service_Request_Io.ServiceRequest.Id.Value = "='" + srId + "'";

            ip.LOVLanguageMode = "LIC";
            ip.ViewMode = "All";

            Stopwatch stopwatch = new Stopwatch();
            SRSVC.WC_Service_Request_BSQueryPage_Output opList;
            try
            {
                using (new OperationContextScope(client.InnerChannel))
                {
                    MessageHeader usrMsgHdr = MessageHeader.CreateHeader("UsernameToken", "http://siebel.com/webservices", SRServiceUsername);
                    OperationContext.Current.OutgoingMessageHeaders.Add(usrMsgHdr);
                    MessageHeader pwdMsgHdr = MessageHeader.CreateHeader("PasswordText", "http://siebel.com/webservices", SRServicePassword);
                    OperationContext.Current.OutgoingMessageHeaders.Add(pwdMsgHdr);
                    stopwatch.Start();
                    opList = client.WC_Service_Request_BSQueryPage(ip);
                    stopwatch.Stop();
                    request = eBehavior.msgInspector.reqPayload;
                    logMessage = "Request of SR detail (Success). Siebel srId = " + srId;
                    logNote = "Request Payload: " + request;
                    log.DebugLog(_logIncidentId, _logContactId, logMessage, logNote);
                    response = eBehavior.msgInspector.resPayload;
                    logMessage = "Response of SR detail (Success). Siebel srId = " + srId;
                    logNote = "Response Payload: " + response;
                    log.DebugLog(_logIncidentId, _logContactId, logMessage, logNote, (int)stopwatch.ElapsedMilliseconds);
                }
            }
            catch (Exception ex)
            {
                opList = client.WC_Service_Request_BSQueryPage(ip);
                request = eBehavior.msgInspector.reqPayload;
                logMessage = "Request of SR detail (Failure). Siebel srId = " + srId;
                logNote = "Request Payload: " + request;
                log.DebugLog(_logIncidentId, _logContactId, logMessage, logNote);
                response = eBehavior.msgInspector.resPayload;
                logMessage = "Response of SR detail (Failure). Siebel srId = " + srId;
                logNote = "Response Payload: " + response;
                log.DebugLog(_logIncidentId, _logContactId, logMessage, logNote);
                handleSiebelException(ex, "client.WC_Contacts_BSQueryPage(ip)");
                throw ex;
            }
            Dictionary<string, string> dictDetail = new Dictionary<string, string>();

            SRSVC.ServiceRequestData srData = opList.ListOfWc_Service_Request_Io.ServiceRequest[0];
            foreach (PropertyInfo propertyInfo in srData.GetType().GetProperties())
            {
                Object propVal = srData.GetType().GetProperty(propertyInfo.Name).GetValue(srData, null);
                dictAddProperty(propertyInfo, propVal, ref dictDetail);
            }

            return dictDetail;
        }
        public ServiceRequest LookupSR(string incident_id, int _logIncidentId = 0, int _logContactId = 0)
        {
            if (String.IsNullOrWhiteSpace(SRURL) || String.IsNullOrWhiteSpace(SRServiceUsername) || String.IsNullOrWhiteSpace(SRServicePassword))
            {
                throw new Exception("Provider's InitForSR not run.");
            }
            string request, response, logMessage, logNote;

            SRSVC.WC_Service_Request_BSClient client = new SRSVC.WC_Service_Request_BSClient(binding, addr);
            MyEndpointBehavior eBehavior = new MyEndpointBehavior();
            client.Endpoint.Behaviors.Add(eBehavior);
            if(SRServiceTimeout > 0)
                client.InnerChannel.OperationTimeout = TimeSpan.FromMilliseconds(SRServiceTimeout );

            SRSVC.WC_Service_Request_BSQueryPage_Input ip = new SRSVC.WC_Service_Request_BSQueryPage_Input();

            ServiceRequest sr = new ServiceRequest();

            ip.ListOfWc_Service_Request_Io = new SRSVC.ListOfWc_Service_Request_IoQuery();
            ip.ListOfWc_Service_Request_Io.ServiceRequest = new SRSVC.ServiceRequestQuery();
            ip.ListOfWc_Service_Request_Io.ServiceRequest.Id = new SRSVC.queryType();
            if (!String.IsNullOrEmpty(incident_id))
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest.Id.Value = "='" + incident_id + "'";
            }
            else
            {
                throw new Exception("Service Request ID is empty.");
            }

            ip.ListOfWc_Service_Request_Io.ServiceRequest.Description = new SRSVC.queryType();
            ip.ListOfWc_Service_Request_Io.ServiceRequest.Description.Value = String.Empty;

            ip.ListOfWc_Service_Request_Io.ServiceRequest.ContactId = new SRSVC.queryType();
            ip.ListOfWc_Service_Request_Io.ServiceRequest.ContactId.Value = String.Empty;

            ip.ListOfWc_Service_Request_Io.ServiceRequest.OwnedById = new SRSVC.queryType();
            ip.ListOfWc_Service_Request_Io.ServiceRequest.OwnedById.Value = String.Empty;

            ip.ListOfWc_Service_Request_Io.ServiceRequest.Owner = new SRSVC.queryType();
            ip.ListOfWc_Service_Request_Io.ServiceRequest.Owner.Value = String.Empty;

            ip.ListOfWc_Service_Request_Io.ServiceRequest.Priority = new SRSVC.queryType();
            ip.ListOfWc_Service_Request_Io.ServiceRequest.Priority.Value = String.Empty;

            ip.ListOfWc_Service_Request_Io.ServiceRequest.SRNumber = new SRSVC.queryType();
            ip.ListOfWc_Service_Request_Io.ServiceRequest.SRNumber.Value = String.Empty;

            ip.ListOfWc_Service_Request_Io.ServiceRequest.SRType = new SRSVC.queryType();
            ip.ListOfWc_Service_Request_Io.ServiceRequest.SRType.Value = String.Empty;

            ip.ListOfWc_Service_Request_Io.ServiceRequest.SerialNumber = new SRSVC.queryType();
            ip.ListOfWc_Service_Request_Io.ServiceRequest.SerialNumber.Value = String.Empty;

            ip.ListOfWc_Service_Request_Io.ServiceRequest.Severity = new SRSVC.queryType();
            ip.ListOfWc_Service_Request_Io.ServiceRequest.Severity.Value = String.Empty;

            ip.ListOfWc_Service_Request_Io.ServiceRequest.Status = new SRSVC.queryType();
            ip.ListOfWc_Service_Request_Io.ServiceRequest.Status.Value = String.Empty;

            ip.ListOfWc_Service_Request_Io.ServiceRequest.Type = new SRSVC.queryType();
            ip.ListOfWc_Service_Request_Io.ServiceRequest.Type.Value = String.Empty;

            ip.ListOfWc_Service_Request_Io.ServiceRequest.ProductId = new SRSVC.queryType();
            ip.ListOfWc_Service_Request_Io.ServiceRequest.ProductId.Value = String.Empty;

            ip.ListOfWc_Service_Request_Io.ServiceRequest.Product = new SRSVC.queryType();
            ip.ListOfWc_Service_Request_Io.ServiceRequest.Product.Value = String.Empty;


            ip.LOVLanguageMode = "LIC";
            ip.ViewMode = "All";
            Stopwatch stopwatch = new Stopwatch();
            try
            {
                SRSVC.WC_Service_Request_BSQueryPage_Output op;
                using (new OperationContextScope(client.InnerChannel))
                {
                    MessageHeader usrMsgHdr = MessageHeader.CreateHeader("UsernameToken", "http://siebel.com/webservices", SRServiceUsername);
                    OperationContext.Current.OutgoingMessageHeaders.Add(usrMsgHdr);
                    MessageHeader pwdMsgHdr = MessageHeader.CreateHeader("PasswordText", "http://siebel.com/webservices", SRServicePassword);
                    OperationContext.Current.OutgoingMessageHeaders.Add(pwdMsgHdr);
                    stopwatch.Start();
                    op = client.WC_Service_Request_BSQueryPage(ip);
                    stopwatch.Stop();
                    request = eBehavior.msgInspector.reqPayload;
                    response = eBehavior.msgInspector.resPayload;

                    SRSVC.ServiceRequestData opData = op.ListOfWc_Service_Request_Io.ServiceRequest[0];
                    sr.RequestID = opData.Id;
                    sr.RequestNumber = opData.SRNumber;
                    //sr.IncidentID
                    //sr.IncidentRef
                    sr.Status = opData.Status;
                    sr.Severity = opData.Severity;
                    sr.RequestType = opData.SRType;
                    sr.Summary = opData.Description;
                    sr.SerialNumber = opData.SerialNumber;
                    sr.Owner = opData.Owner;
                    sr.OwnerID = opData.OwnedById;
                    sr.Product = opData.ProductId;
                    sr.ProductDescription = opData.Product;
                }

                logMessage = "Request of loading Service Request (Success). SR ID = " + incident_id;
                logNote = "Request Payload: " + request;
                log.DebugLog(_logIncidentId, _logContactId, logMessage, logNote);

                logMessage = "Response of loading Service Request (Success). SR ID = " + incident_id;
                logNote = "Response Payload: " + response;
                log.DebugLog(_logIncidentId, _logContactId, logMessage, logNote, (int)stopwatch.ElapsedMilliseconds);
            }
            catch (Exception ex)
            {
                request = eBehavior.msgInspector.reqPayload;
                response = eBehavior.msgInspector.resPayload;

                sr.ErrorMessage = "There has been an error communicating with Siebel. Please check log for detail.";
                logMessage = "Request of loading Service Request (Failure). SR ID = " + incident_id + " Error: " + ex.Message;
                logNote = "Request Payload: " + request;
                log.ErrorLog(_logIncidentId, _logContactId, logMessage, logNote);

                logMessage = "Response of loading Service Request (Failure). SR ID = " + incident_id + " Error: " + ex.Message;
                logNote = "Response Payload: " + response;
                log.ErrorLog(_logIncidentId, _logContactId, logMessage, logNote);

                handleSiebelException(ex, "Lookup Service Request", _logIncidentId, _logContactId);
            }
            return sr;
        }
        // Activity

        public Activity CreateActivity(Activity activity, int _logIncidentId = 0, int _logContactId = 0)
        {
            // reuse SR web service
            if (String.IsNullOrWhiteSpace(SRURL) || String.IsNullOrWhiteSpace(SRServiceUsername)
                || String.IsNullOrWhiteSpace(SRServicePassword))
            {
                throw new Exception("Provider's InitForSR not run.");
            }

            string request, response, logMessage, logNote;

            SRSVC.WC_Service_Request_BSClient client = new SRSVC.WC_Service_Request_BSClient(binding, addr);
            MyEndpointBehavior eBehavior = new MyEndpointBehavior();
            client.Endpoint.Behaviors.Add(eBehavior);
            if (ActivityServiceTimeout > 0)
                client.InnerChannel.OperationTimeout = TimeSpan.FromMilliseconds(ActivityServiceTimeout);

            SRSVC.WC_Service_Request_BSInsert_Input ip = new SRSVC.WC_Service_Request_BSInsert_Input();


            ip.ListOfWc_Service_Request_Io = new SRSVC.ListOfWc_Service_Request_IoData();
            ip.ListOfWc_Service_Request_Io.ServiceRequest = new SRSVC.ServiceRequestData[1];
            ip.ListOfWc_Service_Request_Io.ServiceRequest[0] = new SRSVC.ServiceRequestData();
            if (activity.SrID != null)
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].Id = activity.SrID;
            }
            else
            {
                activity.ErrorMessage = "The following error occurred when doing the Create: SR ID is empty";
                return activity;
            }

            ip.ListOfWc_Service_Request_Io.ServiceRequest[0].ListOfAction = new SRSVC.ListOfActionData();
            ip.ListOfWc_Service_Request_Io.ServiceRequest[0].ListOfAction.Action = new SRSVC.ActionData[1];
            ip.ListOfWc_Service_Request_Io.ServiceRequest[0].ListOfAction.Action[0] = new SRSVC.ActionData();
            if (activity.Comment != null)
            {
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].ListOfAction.Action[0].Description2 = activity.Description;
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].ListOfAction.Action[0].Type = activity.ActivityType;
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].ListOfAction.Action[0].Comment = activity.Comment;
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].ListOfAction.Action[0].Description2 = activity.Description;
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].ListOfAction.Action[0].Due = activity.Due;
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].ListOfAction.Action[0].DueSpecified = true;
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].ListOfAction.Action[0].Priority = activity.Priority;
                ip.ListOfWc_Service_Request_Io.ServiceRequest[0].ListOfAction.Action[0].Status = activity.Status;
            }

            ip.LOVLanguageMode = "LIC";
            ip.ViewMode = "All";
            Stopwatch stopwatch = new Stopwatch();
            try
            {
                SRSVC.WC_Service_Request_BSInsert_Output op;
                using (new OperationContextScope(client.InnerChannel))
                {
                    MessageHeader usrMsgHdr = MessageHeader.CreateHeader("UsernameToken", "http://siebel.com/webservices", SRServiceUsername);
                    OperationContext.Current.OutgoingMessageHeaders.Add(usrMsgHdr);
                    MessageHeader pwdMsgHdr = MessageHeader.CreateHeader("PasswordText", "http://siebel.com/webservices", SRServicePassword);
                    OperationContext.Current.OutgoingMessageHeaders.Add(pwdMsgHdr);
                    stopwatch.Start();
                    op = client.WC_Service_Request_BSInsert(ip);
                    stopwatch.Stop();
                    request = eBehavior.msgInspector.reqPayload;
                    response = eBehavior.msgInspector.resPayload;

                    SRSVC.ServiceRequestId opData = op.ListOfWc_Service_Request_Io[0];
                    activity.ID = opData.ListOfAction[0].Id;
                }
                logMessage = "Request of creating Activity (Success). Created Activity ID = " + activity.ID;
                logNote = "Request Payload: " + request;
                log.DebugLog(_logIncidentId, _logContactId, logMessage, logNote);

                logMessage = "Response of creating Activity(Success). Created Activity ID = " + activity.ID;
                logNote = "Response Payload: " + response;
                log.DebugLog(_logIncidentId, _logContactId, logMessage, logNote, (int)stopwatch.ElapsedMilliseconds);
            }
            catch (Exception ex)
            {
                request = eBehavior.msgInspector.reqPayload;
                response = eBehavior.msgInspector.resPayload;

                activity.ErrorMessage = "There has been an error communicating with Siebel. Please check log for detail.";

                logMessage = "Request of creating  Activity(Failure). " + ex.Message;
                logNote = "Request Payload: " + request;
                log.ErrorLog(_logIncidentId, _logContactId, logMessage, logNote);

                logMessage = "Response of creating  Activity(Failure). " + ex.Message;
                logNote = "Response Payload: " + response;
                log.ErrorLog(_logIncidentId, _logContactId, logMessage, logNote);

                handleSiebelException(ex, "Create  Activity of Service Request", _logIncidentId, _logContactId);
            }
            return activity;
        }