Tuesday, November 26, 2013

some code refactor

Before:

            PAS.UserAuths ret = new PAS.UserAuths();
            PAS.PASservicesSoapClient svc = new PAS.PASservicesSoapClient();
            var securityHeader = new PHSCredentialsHeader()
            {
                Username = _Username,
                Password = _Password
            };

            using (System.ServiceModel.OperationContextScope contextScope = new System.ServiceModel.OperationContextScope(svc.InnerChannel))
            {
                System.ServiceModel.OperationContext.Current.OutgoingMessageHeaders.Add(securityHeader);

                                     ...

After using dll:

            PAS.UserAuths ret = new PAS.UserAuths();
            PAS.PASservicesSoapClient svc = new PAS.PASservicesSoapClient();
 
            PHSServiceAI.PHSServiceWrapper sw = new PHSServiceAI.PHSServiceWrapper(_Username, _Password, svc.InnerChannel);           
 
            using (sw.cScope)
            {
                                ...




DLL Code:

namespace PHSServiceAI
{
    /// 
    /// Security Header Wrapper for PHS AI Servcies
    /// 
    public class PHSServiceWrapper
    {
        public string UserId { getset; }
        public string Password { getset; }
        public System.ServiceModel.OperationContextScope cScope { getset; }
 
        public PHSServiceWrapper()
        {
 
        }
 
        public PHSServiceWrapper(string uName, string pw, System.ServiceModel.IContextChannel ic)
        {
 
            try
            {
 
                this.UserId = uName;
                this.Password = pw;
                PHSCredentialsHeader crdHdr;
 
                crdHdr = new PHSCredentialsHeader()
                {
                    Username = this.UserId,
                    Password = this.Password
                };
 
                cScope = new System.ServiceModel.OperationContextScope(ic);
                System.ServiceModel.OperationContext.Current.OutgoingMessageHeaders.Add(crdHdr);
 
 
            }
            catch (Exception e)
            {
                //TODO log e.Message etc.
            }
 
 
        }
    }
}

No comments: