Ejemplo n.º 1
0
        public Task ActivatingAsync()
        {
            // Build the middleware pipeline for the current tenant

            IApplicationBuilder appBuilder = new ApplicationBuilder(_serviceProvider);

            var orderedMiddlewares = _middlewareProviders
                .SelectMany(p => p.GetMiddlewares())
                .OrderBy(obj => obj.Priority)
                .ToArray();

            foreach (var middleware in orderedMiddlewares)
            {
                middleware.Configure(appBuilder);
            }

            // Orchard is always the last middleware
            appBuilder.UseMiddleware<OrchardMiddleware>();

            var pipeline = appBuilder.Build();

            _routePublisher.Publish(
                _routeProviders.SelectMany(provider => provider.GetRoutes()),
                pipeline
            );

            return Task.CompletedTask;
        }
 public void UseMiddleware_MutlipleInvokeMethods_ThrowsException()
 {
     var mockServiceProvider = new DummyServiceProvider();
     var builder = new ApplicationBuilder(mockServiceProvider);
     builder.UseMiddleware(typeof(MiddlewareMultipleInvokesStub));
     var exception = Assert.Throws<InvalidOperationException>(() => builder.Build());
     Assert.Equal(Resources.FormatException_UseMiddleMutlipleInvokes("Invoke"), exception.Message);
 }
 public void UseMiddleware_NonTaskReturnType_ThrowsException()
 {
     var mockServiceProvider = new DummyServiceProvider();
     var builder = new ApplicationBuilder(mockServiceProvider);
     builder.UseMiddleware(typeof(MiddlewareNonTaskReturnStub));
     var exception = Assert.Throws<InvalidOperationException>(() => builder.Build());
     Assert.Equal(Resources.FormatException_UseMiddlewareNonTaskReturnType("Invoke", nameof(Task)), exception.Message);
 }
        public void UseMiddleware_WithNoParameters_ThrowsException()
        {
            var mockServiceProvider = new DummyServiceProvider();
            var builder = new ApplicationBuilder(mockServiceProvider);
            builder.UseMiddleware(typeof(MiddlewareNoParametersStub));
            var exception = Assert.Throws<InvalidOperationException>(() => builder.Build());

            Assert.Equal(Resources.FormatException_UseMiddlewareNoParameters("Invoke",nameof(HttpContext)), exception.Message); 
        }
        public async Task InvokeMethodWillAllowPerRequestServices()
        {
            var services = new ServiceCollection()
                .AddScoped<ITestService, TestService>()
                .BuildServiceProvider();
            var builder = new ApplicationBuilder(services);
            builder.UseMiddleware<TestMiddleware>();
            var app = builder.Build();

            var ctx1 = new DefaultHttpContext();
            await app(ctx1);

            var testService = ctx1.Items[typeof(ITestService)];
            Assert.IsType<TestService>(testService);
        }