Esempio n. 1
0
        internal static MessagePump CreateServer(string url, string path, RequestDelegate app, TokenProvider tp = null)
        {
            var server = CreatePump(tp);

            server.Features.Get <IServerAddressesFeature>().Addresses.Add(
                UrlPrefix.Create(new Uri(new Uri(url), path).AbsoluteUri, tp != null ? tp : Utilities.CreateTokenProvider()).ToString());
            server.StartAsync(new DummyApplication(app), CancellationToken.None).Wait();
            return(server);
        }
Esempio n. 2
0
        internal static MessagePump CreateDynamicHttpServer(string basePath, out string root, out string baseAddress, Action <AzureRelayOptions> configureOptions, RequestDelegate app, TokenProvider tp = null)
        {
            var rootUri = new Uri(GetRelayUrl());
            var prefix  = UrlPrefix.Create(new Uri(rootUri, basePath).AbsoluteUri, tp);

            root        = rootUri.ToString();
            baseAddress = prefix.FullPrefix;

            var server = CreatePump(CreateTokenProvider());

            server.Features.Get <IServerAddressesFeature>().Addresses.Add(baseAddress);
            configureOptions(server.Listener.Options);
            server.StartAsync(new DummyApplication(app), CancellationToken.None).Wait();
            return(server);
        }
Esempio n. 3
0
        private IServer CreateServer(out string root, RequestDelegate app)
        {
            // TODO: We're just doing this to get a dynamic port. This can be removed later when we add support for hot-adding prefixes.
            var dynamicServer = Utilities.CreateHttpServerReturnRoot("/", out root, app);

            dynamicServer.Dispose();
            var rootUri = new Uri(root);
            var server  = Utilities.CreatePump();

            foreach (string path in new[] { "/", "/11", "/2/3", "/2", "/11/2" })
            {
                server.Listener.Options.UrlPrefixes.Add(UrlPrefix.Create(new Uri(rootUri, path).AbsoluteUri, Utilities.CreateTokenProvider()));
            }

            server.StartAsync(new DummyApplication(app), CancellationToken.None).Wait();
            return(server);
        }
Esempio n. 4
0
        internal static IWebHost CreateDynamicHost(string basePath, out string root, out string baseAddress, Action <AzureRelayOptions> configureOptions, RequestDelegate app, TokenProvider tp = null)
        {
            var prefix = UrlPrefix.Create(new Uri(new Uri(Utilities.GetRelayUrl()), basePath).AbsoluteUri, tp);

            root        = prefix.FullPrefix;
            baseAddress = prefix.ToString();

            var builder = new WebHostBuilder()
                          .UseAzureRelay(options =>
            {
                options.UrlPrefixes.Add(prefix);
                configureOptions(options);
            })
                          .Configure(appBuilder => appBuilder.Run(app));

            var host = builder.Build();

            host.Start();
            return(host);
        }
        public Task StartAsync <TContext>(IHttpApplication <TContext> application, CancellationToken cancellationToken)
        {
            if (application == null)
            {
                throw new ArgumentNullException(nameof(application));
            }

            var hostingUrlsPresent = _serverAddresses.Addresses.Count > 0;

            if (_serverAddresses.PreferHostingUrls && hostingUrlsPresent)
            {
                if (_options.UrlPrefixes.Count > 0)
                {
                    LogHelper.LogWarning(_logger, $"Overriding endpoints added to {nameof(AzureRelayOptions.UrlPrefixes)} since {nameof(IServerAddressesFeature.PreferHostingUrls)} is set to true." +
                                         $" Binding to address(es) '{string.Join(", ", _serverAddresses.Addresses)}' instead. ");

                    Listener.Options.UrlPrefixes.Clear();
                }

                foreach (var value in _serverAddresses.Addresses)
                {
                    Listener.Options.UrlPrefixes.Add(UrlPrefix.Create(value));
                }
            }
            else if (_options.UrlPrefixes.Count > 0)
            {
                if (hostingUrlsPresent)
                {
                    LogHelper.LogWarning(_logger, $"Overriding address(es) '{string.Join(", ", _serverAddresses.Addresses)}'. " +
                                         $"Binding to endpoints added to {nameof(AzureRelayOptions.UrlPrefixes)} instead.");

                    _serverAddresses.Addresses.Clear();
                }

                foreach (var prefix in _options.UrlPrefixes)
                {
                    _serverAddresses.Addresses.Add(prefix.FullPrefix);
                }
            }
            else if (hostingUrlsPresent)
            {
                foreach (var value in _serverAddresses.Addresses)
                {
                    Listener.Options.UrlPrefixes.Add(UrlPrefix.Create(value));
                }
            }
            else
            {
                LogHelper.LogDebug(_logger, $"No listening endpoints were configured.");
                throw new InvalidOperationException("No listening endpoints were configured.");
            }

            // Can't call Start twice
            Contract.Assert(_application == null);
            Contract.Assert(application != null);

            _application = new ApplicationWrapper <TContext>(application);

            Listener.Start();

            return(Task.CompletedTask);
        }