Exemplo n.º 1
0
        private static void ApplyApplicationBuilder(WebApplicationHost source, ApplicationBuilder dest, bool hasRoutesWithoutRouteMiddleware)
        {
            var sourceComponents = GetComponentList(source.ApplicationBuilder);
            var destComponents   = GetComponentList(dest);

            foreach (var item in source.Properties)
            {
                dest.Properties[item.Key] = item.Value;
            }

            // Add the middleware entries
            foreach (var ds in sourceComponents)
            {
                destComponents.Add(ds);
            }

            // Add the endpoint middleware
            if (hasRoutesWithoutRouteMiddleware)
            {
                dest.UseEndpoints(e => { });
            }
        }
Exemplo n.º 2
0
        public WebApplicationHost Build()
        {
            WebApplicationHost webAppHost = null;

            _hostBuilder.ConfigureWebHostDefaults(web =>
            {
                if (_urls != null)
                {
                    web.UseUrls(_urls);
                }

                web.Configure(app =>
                {
                    var hasRoutesWithoutRouteMiddleware = false;

                    // The endpoints were already added on the outside
                    if (webAppHost.DataSources.Count > 0)
                    {
                        // The user did not register the routing middleware
                        if (webAppHost.RouteBuilder == null)
                        {
                            hasRoutesWithoutRouteMiddleware = true;

                            // Add the routing middleware first
                            app.UseRouting();

                            // Copy the routes over
                            var routes = (IEndpointRouteBuilder)app.Properties[WebApplicationHost.EndpointRouteBuilder];
                            foreach (var ds in webAppHost.DataSources)
                            {
                                routes.DataSources.Add(ds);
                            }
                        }
                        else
                        {
                            foreach (var ds in webAppHost.DataSources)
                            {
                                webAppHost.RouteBuilder.DataSources.Add(ds);
                            }

                            webAppHost.UseEndpoints(_ => { });
                        }
                    }

                    ApplyApplicationBuilder(webAppHost, (ApplicationBuilder)app, hasRoutesWithoutRouteMiddleware);
                });
            });

            _hostBuilder.ConfigureServices(services =>
            {
                foreach (var s in Services)
                {
                    services.Add(s);
                }
            });

            _hostBuilder.ConfigureAppConfiguration(builder =>
            {
                foreach (var s in Configuration.Sources)
                {
                    builder.Sources.Add(s);
                }
            });

            _hostBuilder.ConfigureHostConfiguration(builder =>
            {
                foreach (var s in HostConfiguration.Sources)
                {
                    builder.Sources.Add(s);
                }
            });

            var host = _hostBuilder.Build();

            return(webAppHost = new WebApplicationHost(host));
        }