Exemplo n.º 1
0
        public async Task Start()
        {
            try
            {
                var section = (RelayServerSection)ConfigurationManager.GetSection("relayServer");

                if (section.OnPremiseTargets.Count == 0)
                {
                    throw new ConfigurationErrorsException("At least one On-Premise Target needs to be configured.");
                }

                switch (section.Security.AuthenticationType)
                {
                case AuthenticationType.Identity:
                    if (String.IsNullOrEmpty(section.Security.Identity.UserName))
                    {
                        throw new ConfigurationErrorsException(
                                  "The user name cannot be null or empty when using authentication type 'Identity'.");
                    }

                    _connector = new RelayServerConnector(section.Security.Identity.UserName,
                                                          section.Security.Identity.Password, new Uri(section.BaseUrl),
                                                          (int)section.RequestTimeout.TotalSeconds);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                _connector.RelayedRequestHeader = "X-Relayed";

                foreach (var onPremiseTarget in section.OnPremiseTargets.Cast <OnPremiseTargetElement>())
                {
                    _connector.RegisterOnPremiseTarget(onPremiseTarget.Key, new Uri(onPremiseTarget.BaseUrl));
                }


                await _connector.Connect();
            }
            catch (Exception e)
            {
                _logger.FatalException("Fatal exception occured", e);
                throw;
            }
        }