当前位置: 首页>>代码示例>>C#>>正文


C# ServiceCollection.AddMvc方法代码示例

本文整理汇总了C#中Microsoft.Framework.DependencyInjection.ServiceCollection.AddMvc方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.AddMvc方法的具体用法?C# ServiceCollection.AddMvc怎么用?C# ServiceCollection.AddMvc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.Framework.DependencyInjection.ServiceCollection的用法示例。


在下文中一共展示了ServiceCollection.AddMvc方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MultiRegistrationServiceTypes_AreRegistered_MultipleTimes

        public void MultiRegistrationServiceTypes_AreRegistered_MultipleTimes()
        {
            // Arrange
            var services = new ServiceCollection();

            // Register a mock implementation of each service, AddMvcServices should add another implemenetation.
            foreach (var serviceType in MutliRegistrationServiceTypes)
            {
                var mockType = typeof(Mock<>).MakeGenericType(serviceType.Key);
                services.Add(ServiceDescriptor.Transient(serviceType.Key, mockType));
            }

            // Act
            services.AddMvc();

            // Assert
            foreach (var serviceType in MutliRegistrationServiceTypes)
            {
                AssertServiceCountEquals(services, serviceType.Key, serviceType.Value.Length + 1);

                foreach (var implementationType in serviceType.Value)
                {
                    AssertContainsSingle(services, serviceType.Key, implementationType);
                }
            }
        }
开发者ID:4myBenefits,项目名称:Mvc,代码行数:26,代码来源:MvcServiceCollectionExtensionsTest.cs

示例2: Load

            protected override void Load(ContainerBuilder builder)
            {
                var services2 = new ServiceCollection();
                services2.AddMvc();
                services2.ConfigureRazorViewEngine(options =>
                {
                    options.ViewLocationExpanders.Add(new TestViewLocationExpander());
                });

                builder.Populate(services2);
            }
开发者ID:pranavkm,项目名称:AutofacSample,代码行数:11,代码来源:Startup.cs

示例3: ShoppingCartControllerTest

        public ShoppingCartControllerTest()
        {
            var services = new ServiceCollection();

            services.AddEntityFramework()
                      .AddInMemoryStore()
                      .AddDbContext<MusicStoreContext>();

            services.AddMvc();

            _serviceProvider = services.BuildServiceProvider();
        }
开发者ID:alexandreana,项目名称:MusicStore,代码行数:12,代码来源:ShoppingCartControllerTest.cs

示例4: Load

            protected override void Load(ContainerBuilder builder)
            {
                Console.WriteLine("Registering Module");

                var services2 = new ServiceCollection();
                services2.AddMvc();
                services2.ConfigureRazorViewEngine(options => {
                    Console.WriteLine("Registering TestViewLocationExpander");
                    options.ViewLocationExpanders.Add(new TestViewLocationExpander());
                });

                builder.Populate(services2);
            }
开发者ID:Jetski5822,项目名称:AutofacSample,代码行数:13,代码来源:Startup.cs

示例5: CreateContainer

        public IServiceProvider CreateContainer(ShellSettings settings, ShellBlueprint blueprint) {
            ServiceCollection serviceCollection = new ServiceCollection();

            serviceCollection.AddScoped<IOrchardShell, DefaultOrchardShell>();
            serviceCollection.AddScoped<IRouteBuilder, DefaultShellRouteBuilder>();
            serviceCollection.AddInstance(settings);
            serviceCollection.AddInstance(blueprint.Descriptor);
            serviceCollection.AddInstance(blueprint);

            serviceCollection.AddMvc();

            serviceCollection.Configure<RazorViewEngineOptions>(options => {
                var expander = new ModuleViewLocationExpander();
                options.ViewLocationExpanders.Add(expander);
            });

            var p = _serviceProvider.GetService<IOrchardLibraryManager>();
            serviceCollection.AddInstance<IAssemblyProvider>(new DefaultAssemblyProviderTest(p, _serviceProvider, _serviceProvider.GetService<IAssemblyLoaderContainer>()));

            foreach (var dependency in blueprint.Dependencies) {
                foreach (var interfaceType in dependency.Type.GetInterfaces()
                    .Where(itf => typeof(IDependency).IsAssignableFrom(itf))) {
                    Logger.Debug("Type: {0}, Interface Type: {1}", dependency.Type, interfaceType);

                    if (typeof(ISingletonDependency).IsAssignableFrom(interfaceType)) {
                        serviceCollection.AddSingleton(interfaceType, dependency.Type);
                    }
                    else if (typeof(IUnitOfWorkDependency).IsAssignableFrom(interfaceType)) {
                        serviceCollection.AddScoped(interfaceType, dependency.Type);
                    }
                    else if (typeof (ITransientDependency).IsAssignableFrom(interfaceType)) {
                        serviceCollection.AddTransient(interfaceType, dependency.Type);
                    }
                    else {
                        serviceCollection.AddScoped(interfaceType, dependency.Type);
                    }
                }
            }

            //foreach (var item in blueprint.Controllers) {
            //    var serviceKeyName = (item.AreaName + "/" + item.ControllerName).ToLowerInvariant();
            //    var serviceKeyType = item.Type;
            //    serviceCollection.AddScoped(serviceKeyType);

            //}

            return BuildFallbackServiceProvider(
                            serviceCollection,
                            _serviceProvider);
        }
开发者ID:night-king,项目名称:OrchardNoCMS,代码行数:50,代码来源:ShellContainerFactory.cs

示例6: UseScriptConsole

        public static void UseScriptConsole(this IApplicationBuilder app, IServiceCollection theServices)
        {
            var appS = app.ApplicationServices;
            var scriptManager = new ScriptManager();

            var services = new ServiceCollection();
            services.Clear();
            foreach (var s in theServices)
               services.Insert(0, s);

            services.AddInstance<ScriptManager>(scriptManager);
            var fp = new DebugFileProvider(new EmbeddedFileProvider(typeof(ScriptConsoleBuilderExtensions).Assembly, "ScriptConsole"));

            services
                .AddMvc()
                .AddControllersAsServices(new[] { typeof(ScriptConsoleController), typeof(HomeController) })
                .AddRazorOptions(r => r.FileProvider = fp);

            services.AddLogging();
            var provider = services.BuildServiceProvider();

            app.Map("/ScriptConsole", builder =>
            {
                var routeBuilder = new RouteBuilder()
                {
                    DefaultHandler = new MvcRouteHandler(),
                    ServiceProvider = new ShadowedServiceProvider(provider, app.ApplicationServices)
                };
                routeBuilder.MapRoute("ScriptConsole", "{action}", new { controller = "ScriptConsole", action = "Index" });
                routeBuilder.MapRoute("ScriptConsoleX", "{controller}/{action}", new { controller = "ScriptConsole", action = "Index" });

                var route = routeBuilder.Build();
                builder.Use(next =>
                {
                    return async (context) =>
                    {
                        context.ApplicationServices = new ShadowedServiceProvider(provider, context.ApplicationServices);
                        context.RequestServices = new ShadowedServiceProvider(provider, context.RequestServices);
                        await route.RouteAsync(new RouteContext(context));
                    };
                });

            });
        }
开发者ID:MarkPflug,项目名称:AspNetScriptConsole,代码行数:44,代码来源:ScriptConsoleBuilderExtensions.cs

示例7: SingleRegistrationServiceTypes_AreNotRegistered_MultipleTimes

        public void SingleRegistrationServiceTypes_AreNotRegistered_MultipleTimes()
        {
            // Arrange
            var services = new ServiceCollection();

            // Register a mock implementation of each service, AddMvcServices should not replace it.
            foreach (var serviceType in SingleRegistrationServiceTypes)
            {
                var mockType = typeof(Mock<>).MakeGenericType(serviceType);
                services.Add(ServiceDescriptor.Transient(serviceType, mockType));
            }

            // Act
            services.AddMvc();

            // Assert
            foreach (var singleRegistrationType in SingleRegistrationServiceTypes)
            {
                AssertServiceCountEquals(services, singleRegistrationType, 1);
            }
        }
开发者ID:4myBenefits,项目名称:Mvc,代码行数:21,代码来源:MvcServiceCollectionExtensionsTest.cs

示例8: InitializeServices

        private static void InitializeServices(HttpContext httpContext, Action<MvcOptions> updateOptions = null)
        {
            var serviceCollection = new ServiceCollection();
            serviceCollection.AddMvc();

            httpContext.RequestServices = serviceCollection.BuildServiceProvider();

            var actionContext = new ActionContext(httpContext, new RouteData(), new ControllerActionDescriptor());

            var actionContextAccessor =
                httpContext.RequestServices.GetRequiredService<IScopedInstance<ActionContext>>();
            actionContextAccessor.Value = actionContext;

            var options = new TestMvcOptions().Options;
            if (updateOptions != null)
            {
                updateOptions(options);
            }

            var actionBindingContextAccessor =
                httpContext.RequestServices.GetRequiredService<IScopedInstance<ActionBindingContext>>();
            actionBindingContextAccessor.Value = GetActionBindingContext(options, actionContext);
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:23,代码来源:ModelBindingTestHelper.cs

示例9: Configure

        public void Configure(IBuilder app)
        {
            app.UseFileServer();
#if NET45
            var configuration = new Configuration()
                                    .AddJsonFile(@"App_Data\config.json")
                                    .AddEnvironmentVariables();

            string diSystem;

            if (configuration.TryGet("DependencyInjection", out diSystem) && 
                diSystem.Equals("AutoFac", StringComparison.OrdinalIgnoreCase))
            {
                app.UseMiddleware<MonitoringMiddlware>();

                var services = new ServiceCollection();

                services.AddMvc();
                services.AddSingleton<PassThroughAttribute>();
                services.AddSingleton<UserNameService>();
                services.AddTransient<ITestService, TestService>();                
                services.Add(OptionsServices.GetDefaultServices());

                // Create the autofac container 
                ContainerBuilder builder = new ContainerBuilder();

                // Create the container and use the default application services as a fallback 
                AutofacRegistration.Populate(
                    builder,
                    services,
                    fallbackServiceProvider: app.ApplicationServices);

                builder.RegisterModule<MonitoringModule>();

                IContainer container = builder.Build();

                app.UseServices(container.Resolve<IServiceProvider>());
            }
            else
#endif
            {
                app.UseServices(services =>
                {
                    services.AddMvc();
                    services.AddSingleton<PassThroughAttribute>();
                    services.AddSingleton<UserNameService>();
                    services.AddTransient<ITestService, TestService>();
                });
            }

            app.UseMvc(routes =>
            {
                routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}");

                routes.MapRoute(
                    "controllerActionRoute",
                    "{controller}/{action}",
                    new { controller = "Home", action = "Index" });

                routes.MapRoute(
                    "controllerRoute",
                    "{controller}",
                    new { controller = "Home" });
            });
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:65,代码来源:Startup.cs

示例10: AddMvcServicesTwice_DoesNotAddDuplicates

        public void AddMvcServicesTwice_DoesNotAddDuplicates()
        {
            // Arrange
            var services = new ServiceCollection();

            // Act
            services.AddMvc();
            services.AddMvc();

            // Assert
            var singleRegistrationServiceTypes = SingleRegistrationServiceTypes;
            foreach (var service in services)
            {
                if (singleRegistrationServiceTypes.Contains(service.ServiceType))
                {
                    // 'single-registration' services should only have one implementation registered.
                    AssertServiceCountEquals(services, service.ServiceType, 1);
                }
                else if (service.ImplementationType != null && !service.ImplementationType.Assembly.FullName.Contains("Mvc"))
                {
                    // Ignore types that don't come from MVC
                }
                else
                {
                    // 'multi-registration' services should only have one *instance* of each implementation registered.
                    AssertContainsSingle(services, service.ServiceType, service.ImplementationType);
                }
            }
        }
开发者ID:notami18,项目名称:Mvc,代码行数:29,代码来源:MvcServiceCollectionExtensionsTest.cs


注:本文中的Microsoft.Framework.DependencyInjection.ServiceCollection.AddMvc方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。