public void GlobalSetup()
        {
            var loader        = new RazorCompiledItemLoader();
            var viewsDll      = Path.ChangeExtension(typeof(ViewAssemblyMarker).Assembly.Location, "Views.dll");
            var viewsAssembly = Assembly.Load(File.ReadAllBytes(viewsDll));
            var services      = new ServiceCollection();
            var listener      = new DiagnosticListener(GetType().Assembly.FullName);
            var partManager   = new ApplicationPartManager();

            partManager.ApplicationParts.Add(CompiledRazorAssemblyApplicationPartFactory.GetDefaultApplicationParts(viewsAssembly).Single());
            var builder = services
                          .AddSingleton <ILoggerFactory, NullLoggerFactory>()
                          .AddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>()
                          .AddSingleton <DiagnosticSource>(listener)
                          .AddSingleton(listener)
                          .AddSingleton <IWebHostEnvironment, BenchmarkHostingEnvironment>()
                          .AddSingleton <ApplicationPartManager>(partManager)
                          .AddScoped <BenchmarkViewExecutor>()
                          .AddMvc();

            _serviceProvider           = services.BuildServiceProvider();
            _routeData                 = new RouteData();
            _actionDescriptor          = new ActionDescriptor();
            _tempDataDictionaryFactory = _serviceProvider.GetRequiredService <ITempDataDictionaryFactory>();
            _viewEngine                = _serviceProvider.GetRequiredService <ICompositeViewEngine>();
        }
Ejemplo n.º 2
0
    public void ConfigureServices(IServiceCollection services)
    {
        var builder = services
                      .AddControllersWithViews()
                      .ConfigureApplicationPartManager(manager => manager.ApplicationParts.Clear())
                      .AddApplicationPart(typeof(TimeScheduleController).GetTypeInfo().Assembly)
                      .ConfigureApplicationPartManager(manager =>
        {
            manager.ApplicationParts.Add(new TypesPart(
                                             typeof(AnotherController),
                                             typeof(ComponentFromServicesViewComponent),
                                             typeof(InServicesTagHelper)));

            foreach (var part in CompiledRazorAssemblyApplicationPartFactory.GetDefaultApplicationParts(Assembly.GetExecutingAssembly()))
            {
                manager.ApplicationParts.Add(part);
            }
        })
                      .AddControllersAsServices()
                      .AddViewComponentsAsServices()
                      .AddTagHelpersAsServices();

        services.AddTransient <QueryValueService>();
        services.AddTransient <ValueService>();
        services.AddHttpContextAccessor();
    }
Ejemplo n.º 3
0
        public void ConfigureServices(IServiceCollection services)
        {
            var builder = services
                          .AddMvc()
                          .ConfigureApplicationPartManager(manager => manager.ApplicationParts.Clear())
                          .AddApplicationPart(typeof(TimeScheduleController).GetTypeInfo().Assembly)
                          .ConfigureApplicationPartManager(manager =>
            {
                manager.ApplicationParts.Add(new TypesPart(
                                                 typeof(AnotherController),
                                                 typeof(ComponentFromServicesViewComponent),
                                                 typeof(InServicesTagHelper)));

                var relatedAssenbly = RelatedAssemblyAttribute
                                      .GetRelatedAssemblies(GetType().Assembly, throwOnError: true)
                                      .SingleOrDefault();
                foreach (var part in CompiledRazorAssemblyApplicationPartFactory.GetDefaultApplicationParts(relatedAssenbly))
                {
                    manager.ApplicationParts.Add(part);
                }
            })
                          .AddControllersAsServices()
                          .AddViewComponentsAsServices()
                          .AddTagHelpersAsServices()
                          .SetCompatibilityVersion(CompatibilityVersion.Latest);

            services.AddTransient <QueryValueService>();
            services.AddTransient <ValueService>();
            services.AddHttpContextAccessor();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Checks if the precompiled razor views have been added as an
        /// ApplicationPart. If not, adds them.
        /// On .NET 461, Microsoft.Fx.Portability.Reports.Html.dll
        /// and Microsoft.Fx.Portability.Reports.Html.Views.dll are
        /// not added to the ApplicationPartsManager by default.
        /// For more information: https://github.com/aspnet/Razor/issues/2262.
        /// </summary>
        private static void ConfigureRazorViews(ApplicationPartManager partsManager, IFileProvider fileProvider)
        {
            // Using StringComparison.OrdinalIgnoreCase because on .NET FX,
            // binaries are output as .DLL sometimes rather than .dll, which
            // leaves us unable to find the files.
            var comparison = StringComparison.OrdinalIgnoreCase;

#if !NET461
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                comparison = StringComparison.Ordinal;
            }
#endif

            var assemblyName = typeof(HtmlRazorReportWriter).Assembly.GetName().Name;
            if (partsManager.ApplicationParts.Any(x => string.Equals(x.Name, assemblyName, comparison)))
            {
                return;
            }

            var applicationParts = fileProvider.GetDirectoryContents(string.Empty)
                                   .Where(x => x.Exists &&
                                          Path.GetExtension(x.Name).Equals(".dll", comparison) &&
                                          x.Name.StartsWith(assemblyName, comparison))
                                   .SelectMany(file =>
            {
                var assembly = Assembly.LoadFrom(file.PhysicalPath);

                return(file.Name.EndsWith("Views.dll", comparison)
                        ? CompiledRazorAssemblyApplicationPartFactory.GetDefaultApplicationParts(assembly)
                        : new[] { new AssemblyPart(assembly) });
            });

            foreach (var part in applicationParts)
            {
                partsManager.ApplicationParts.Add(part);
            }
        }
Ejemplo n.º 5
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews()
            .ConfigureApplicationPartManager(apm =>
            {
                var pluginsPath   = Path.Combine(WebHostEnvironment.ContentRootPath, "Plugins");
                var assemblyFiles = Directory.GetFiles(pluginsPath, "*WebPart.dll", SearchOption.AllDirectories);

                foreach (var assemblyFile in assemblyFiles)
                {
                    var a = Assembly.LoadFrom(assemblyFile);
                    apm.ApplicationParts.Add(new AssemblyPart(a));
                    var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(a, throwOnError: true);
                    foreach (ApplicationPart view in relatedAssemblies.SelectMany(s => CompiledRazorAssemblyApplicationPartFactory.GetDefaultApplicationParts(s)))
                    {
                        apm.ApplicationParts.Add(view);
                    }
                }
            });
        }
 /// <inheritdoc />
 public override IEnumerable <ApplicationPart> GetApplicationParts(Assembly assembly)
 {
     return(Enumerable.Concat(
                DefaultApplicationPartFactory.GetDefaultApplicationParts(assembly),
                CompiledRazorAssemblyApplicationPartFactory.GetDefaultApplicationParts(assembly)));
 }