public ShellApplication(object password, UserSessionService userSessionService, EventWaitHandle loadedEvent, string securityTokenXml) { if (password != null) { _password = SecureStringHelper.GetSecureString(Marshal.PtrToStringBSTR((IntPtr)password)); SecureStringHelper.FreeString((IntPtr)password); } _userSessionService = userSessionService; _loadedEvent = loadedEvent; if (securityTokenXml != null) { _tokenCache = Imi.Framework.UX.Identity.SecurityTokenCache.Deserialize(securityTokenXml); } else { _tokenCache = new Imi.Framework.UX.Identity.SecurityTokenCache(); } _settingsLoadedEvent = new EventWaitHandle(false, EventResetMode.AutoReset); Thread.CurrentThread.CurrentUICulture = userSessionService.UICulture; Run(); }
private object CreateChannelPool(Type channelType) { lock (_channelPoolDictionary) { if (!_channelPoolDictionary.ContainsKey(channelType)) { string serviceName = channelType.Name; string baseAddress = null; object[] attributes = channelType.GetCustomAttributes(typeof(ServiceApplicationNameAttribute), false); if (attributes.Count() > 0) { ServiceApplicationNameAttribute applicationNameAttribute = attributes[0] as ServiceApplicationNameAttribute; baseAddress = applicationNameAttribute.ApplicationName; } Type factoryType = typeof(CustomChannelFactory <>); Type[] typeArgs = { channelType }; Type genericFactoryType = factoryType.MakeGenericType(typeArgs); object[] args = { serviceName, AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, baseAddress, _userSessionService.HostName, _userSessionService.HostPort }; /* Create the ChannelFactory */ ChannelFactory factory = Activator.CreateInstance(genericFactoryType, args) as ChannelFactory; factory.Credentials.UserName.UserName = _userSessionService.DomainUser; if (_userSessionService.Password != null) { factory.Credentials.Windows.ClientCredential.UserName = _userSessionService.UserId; factory.Credentials.Windows.ClientCredential.Domain = _userSessionService.Domain; factory.Credentials.Windows.ClientCredential.SecurePassword = _userSessionService.Password; IntPtr password = SecureStringHelper.GetString(_userSessionService.Password); factory.Credentials.UserName.Password = Marshal.PtrToStringAuto(password); SecureStringHelper.FreeString(password); } ClientCredentials clientCredentials = factory.Endpoint.Behaviors.Remove <ClientCredentials>(); CachedClientCredentials cachedClientCredentials = new CachedClientCredentials(_tokenCache, clientCredentials); factory.Endpoint.Behaviors.Add(cachedClientCredentials); //Set behavior for FaultException size. //To be able to handle large exception messages. factory.Endpoint.Behaviors.Add(new SetMaxFaultSizeBehavior(100000000)); Type channelPoolType = typeof(ChannelPool <>); Type genericChannelPoolType = channelPoolType.MakeGenericType(typeArgs); object[] args2 = { _maxChannelPoolSize, factory }; /* Create the ChannelFactory */ object channelPool = Activator.CreateInstance(genericChannelPoolType, args2); _channelPoolDictionary[channelType] = channelPool; } return(_channelPoolDictionary[channelType]); } }
private void StartShell() { AppDomain domain = null; IntPtr password = default(IntPtr); bool logout = false; try { AppDomainSetup setup = new AppDomainSetup(); setup.ConfigurationFile = UserSessionService.ConfigFilename; setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; string assemblyName = "Imi.SupplyChain.UX.Shell"; string instanceTypeName = "Imi.SupplyChain.UX.Shell.ShellApplication"; domain = AppDomain.CreateDomain("login", null, setup, new PermissionSet(PermissionState.Unrestricted)); string securityTokenXml = null; if (_tokenCache.RawToken != null) { securityTokenXml = SecurityTokenCache.Serialize(_tokenCache); } if (UserSessionService.Password != null) { password = SecureStringHelper.GetString(UserSessionService.Password); UserSessionService.Password = null; } object[] args = new object[4] { password, UserSessionService, _loadedEvent, securityTokenXml }; CleanTempFiles(); ThreadPool.QueueUserWorkItem(WaitForShellStartCallback); domain.CreateInstance(assemblyName, instanceTypeName, true, BindingFlags.CreateInstance, null, args, null, null); password = default(IntPtr); logout = domain.GetData("Logout") != null; } catch (ThreadAbortException) { } catch (Exception ex) { EventLog.WriteEntry(StringResources.Title, ex.ToString(), EventLogEntryType.Error); throw; } finally { if (password != default(IntPtr)) { SecureStringHelper.FreeString(password); } if (domain != null) { try { AppDomain.Unload(domain); } catch (CannotUnloadAppDomainException) { } finally { domain = null; } } try { File.Delete(UserSessionService.ConfigFilename); } catch (Exception) { } CleanTempFiles(); if (logout) { _loginWindow.Dispatcher.Invoke(new Action(() => { ShowLogin(true); _loginWindow.Show(); })); } else { _loginWindow.Closing -= LoginWindowClosingEventHandler; _loginWindow.Dispatcher.Invoke(new Action(() => { _loginWindow.Close(); })); } } }