public void UseSwaggerDocumentationTest()
        {
            // Arrange
            IServiceCollection services = new ServiceCollection();

            SwaggerServiceExtensions.AddSwaggerDocumentation(services);
            ServiceProvider     serviceProvider = services.BuildServiceProvider();
            IApplicationBuilder app             = new Microsoft.AspNetCore.Builder.ApplicationBuilder(serviceProvider);
            string error = null;

            // Act
            try
            {
                SwaggerServiceExtensions.UseSwaggerDocumentation(app);
            }
            catch (Exception ex)
            {
                error = ex.ToString();
            }

            // Assert
            Assert.IsNull(error);
        }
        public async Task UseRouter_IRouter_CallsRoute()
        {
            // Arrange
            var services = CreateServices();

            var app = new ApplicationBuilder(services);

            var router = new Mock <IRouter>(MockBehavior.Strict);

            router
            .Setup(r => r.RouteAsync(It.IsAny <RouteContext>()))
            .Returns(Task.CompletedTask)
            .Verifiable();

            app.UseRouter(router.Object);

            var appFunc = app.Build();

            // Act
            await appFunc(new DefaultHttpContext());

            // Assert
            router.Verify();
        }
示例#3
0
 internal WebApplication(IHost host)
 {
     _host = host;
     ApplicationBuilder = new ApplicationBuilder(host.Services);
     Logger             = host.Services.GetRequiredService <ILoggerFactory>().CreateLogger(Environment.ApplicationName);
 }
示例#4
0
        public void UseEndpoints_CallWithBuilder_SetsEndpointDataSource_WithMap()
        {
            // Arrange
            var matcherEndpointDataSources = new List <EndpointDataSource>();
            var matcherFactoryMock         = new Mock <MatcherFactory>();

            matcherFactoryMock
            .Setup(m => m.CreateMatcher(It.IsAny <EndpointDataSource>()))
            .Callback((EndpointDataSource arg) =>
            {
                matcherEndpointDataSources.Add(arg);
            })
            .Returns(new TestMatcher(false));

            var services = CreateServices(matcherFactoryMock.Object);

            var app = new ApplicationBuilder(services);

            // Act
            app.UseRouting();

            app.Map("/foo", b =>
            {
                b.UseRouting();
                b.UseEndpoints(builder =>
                {
                    builder.Map("/1", d => null).WithDisplayName("Test endpoint 1");
                    builder.Map("/2", d => null).WithDisplayName("Test endpoint 2");
                });
            });

            app.UseEndpoints(builder =>
            {
                builder.Map("/3", d => null).WithDisplayName("Test endpoint 3");
                builder.Map("/4", d => null).WithDisplayName("Test endpoint 4");
            });

            // This triggers the middleware to be created and the matcher factory to be called
            // with the datasource we want to test
            var requestDelegate = app.Build();

            requestDelegate(new DefaultHttpContext());
            requestDelegate(new DefaultHttpContext()
            {
                Request = { Path = "/Foo", },
            });

            // Assert
            Assert.Equal(2, matcherEndpointDataSources.Count);

            // Each UseRouter has its own data source
            Assert.Collection(matcherEndpointDataSources[1].Endpoints, // app.UseRouter
                              e => Assert.Equal("Test endpoint 1", e.DisplayName),
                              e => Assert.Equal("Test endpoint 2", e.DisplayName));

            Assert.Collection(matcherEndpointDataSources[0].Endpoints, // b.UseRouter
                              e => Assert.Equal("Test endpoint 3", e.DisplayName),
                              e => Assert.Equal("Test endpoint 4", e.DisplayName));

            var compositeEndpointBuilder = services.GetRequiredService <EndpointDataSource>();

            // Global middleware has all endpoints
            Assert.Collection(compositeEndpointBuilder.Endpoints,
                              e => Assert.Equal("Test endpoint 1", e.DisplayName),
                              e => Assert.Equal("Test endpoint 2", e.DisplayName),
                              e => Assert.Equal("Test endpoint 3", e.DisplayName),
                              e => Assert.Equal("Test endpoint 4", e.DisplayName));
        }
示例#5
0
 private ApplicationBuilder(ApplicationBuilder builder)
 {
     Properties = new CopyOnWriteDictionary <string, object?>(builder.Properties, StringComparer.Ordinal);
 }
示例#6
0
        /// <summary>
        /// Creates a new isolated application builder which gets its own <see cref="ServiceCollection"/>, which only
        /// has the default services registered. It will not share the <see cref="ServiceCollection"/> from the
        /// originating app.
        /// </summary>
        /// <param name="app">The application builder to create the isolated app from.</param>
        /// <param name="configuration">The branch of the isolated app.</param>
        /// <param name="registration">A method to configure the newly created service collection.</param>
        /// <returns>The new pipeline with the isolated application integrated.</returns>
        public static IApplicationBuilder Isolate(
            this IApplicationBuilder app,
            Action <IApplicationBuilder> configuration,
            Func <IServiceCollection, IServiceProvider> registration)
        {
            var services = CreateDefaultServiceCollection(app.ApplicationServices);
            var provider = registration(services);

            var builder = new ApplicationBuilder(null);

            builder.ApplicationServices = provider;

            builder.Use(async(context, next) =>
            {
                var factory = provider.GetRequiredService <IServiceScopeFactory>();

                // Store the original request services in the current ASP.NET context.
                context.Items[typeof(IServiceProvider)] = context.RequestServices;

                try
                {
                    using (var scope = factory.CreateScope())
                    {
                        context.RequestServices = scope.ServiceProvider;

                        await next();
                    }
                }

                finally
                {
                    context.RequestServices = null;
                }
            });

            configuration(builder);

            return(app.Use(next =>
            {
                // Run the rest of the pipeline in the original context,
                // with the services defined by the parent application builder.
                builder.Run(async context =>
                {
                    var factory = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>();

                    try
                    {
                        using (var scope = factory.CreateScope())
                        {
                            context.RequestServices = scope.ServiceProvider;

                            await next(context);
                        }
                    }

                    finally
                    {
                        context.RequestServices = null;
                    }
                });

                var branch = builder.Build();

                return context => branch(context);
            }));
        }