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


C# IServiceCollection.BuildServiceProvider方法代码示例

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


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

示例1: ConfigureServices

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddCaching();
            services.AddSession();

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

            services.ConfigureMvc(options =>
            {
                options.Filters.Add(typeof(PassThroughAttribute), order: 17);
                options.AddXmlDataContractSerializerFormatter();
                options.Filters.Add(new FormatFilterAttribute());
            });

#if DNX451
            // Fully-qualify configuration path to avoid issues in functional tests. Just "config.json" would be fine
            // but Configuration uses CallContextServiceLocator.Locator.ServiceProvider to get IApplicationEnvironment.
            // Functional tests update that service but not in the static provider.
            var applicationEnvironment = services.BuildServiceProvider().GetRequiredService<IApplicationEnvironment>();
            var configurationPath = Path.Combine(applicationEnvironment.ApplicationBasePath, "config.json");

            // Set up configuration sources.
            var configuration = new Configuration()
                .AddJsonFile(configurationPath)
                .AddEnvironmentVariables();
            string diSystem;
            if (configuration.TryGet("DependencyInjection", out diSystem) &&
                diSystem.Equals("AutoFac", StringComparison.OrdinalIgnoreCase))
            {
                _autoFac = true;
                services.ConfigureRazorViewEngine(options =>
                {
                    var expander = new LanguageViewLocationExpander(
                        context => context.HttpContext.Request.Query["language"]);
                    options.ViewLocationExpanders.Insert(0, expander);
                });

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

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

                builder.RegisterModule<MonitoringModule>();

                var container = builder.Build();

                return container.Resolve<IServiceProvider>();
            }
            else
#endif
            {
                return services.BuildServiceProvider();
            }
        }
开发者ID:AndersBillLinden,项目名称:Mvc,代码行数:60,代码来源:Startup.cs

示例2: ConfigureServices

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddCaching();
            services.AddSession();

            services.AddMvc(options =>
            {
                options.Filters.Add(typeof(PassThroughAttribute), order: 17);
                options.Filters.Add(new FormatFilterAttribute());
            })
            .AddXmlDataContractSerializerFormatters()
            .AddViewLocalization(LanguageViewLocationExpanderFormat.SubFolder);

            services.AddSingleton<PassThroughAttribute>();
            services.AddSingleton<UserNameService>();
            services.AddTransient<ITestService, TestService>();
            

            var applicationEnvironment = services.BuildServiceProvider().GetRequiredService<IApplicationEnvironment>();
            var configurationPath = Path.Combine(applicationEnvironment.ApplicationBasePath, "config.json");

            // Set up configuration sources.
            var configBuilder = new ConfigurationBuilder()
                .AddJsonFile(configurationPath)
                .AddEnvironmentVariables();

            var configuration = configBuilder.Build();

            var diSystem = configuration["DependencyInjection"];
            if (!string.IsNullOrEmpty(diSystem) &&
                diSystem.Equals("AutoFac", StringComparison.OrdinalIgnoreCase))
            {
                _autoFac = true;

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

                // Create the container and use the default application services as a fallback
                builder.Populate(services);

                builder.RegisterModule<MonitoringModule>();

                var container = builder.Build();

                return container.Resolve<IServiceProvider>();
            }
            else
            {
                return services.BuildServiceProvider();
            }
        }
开发者ID:ryanbrandenburg,项目名称:Mvc,代码行数:51,代码来源:Startup.cs

示例3: AssertCorrectDbContextAndOptions

        private void AssertCorrectDbContextAndOptions(IServiceCollection services)
        {
            var serviceProvider = services.BuildServiceProvider();

            var dbContextService = services.FirstOrDefault(s => s.ServiceType == typeof(CustomDbContext));

            Assert.NotNull(dbContextService);
            Assert.Equal(ServiceLifetime.Scoped, dbContextService.Lifetime);

            var customDbContext = serviceProvider.GetService<CustomDbContext>();

            Assert.NotNull(customDbContext);

            var dbContextOptions = serviceProvider.GetService<DbContextOptions<CustomDbContext>>();

            Assert.NotNull(dbContextOptions);
            Assert.Equal(3, dbContextOptions.Extensions.Count());

            var coreOptionsExtension = dbContextOptions.FindExtension<CoreOptionsExtension>();
            var inMemoryOptionsExtension = dbContextOptions.FindExtension<InMemoryOptionsExtension>();
            var scopedInMemoryOptionsExtension = dbContextOptions.FindExtension<ScopedInMemoryOptionsExtension>();

            Assert.NotNull(coreOptionsExtension);
            Assert.NotNull(inMemoryOptionsExtension);
            Assert.NotNull(scopedInMemoryOptionsExtension);
        }
开发者ID:Cream2015,项目名称:MyTested.AspNetCore.Mvc,代码行数:26,代码来源:ServicesTests.cs

示例4: ConfigureServices

        // This method gets called by a runtime.
        // Use this method to add services to the container
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();


            var path = _app.ApplicationBasePath;
            var config = new ConfigurationBuilder()
            .AddJsonFile($"{path}/config.json")
            .Build();

            string typeName = config.Get<string>("RepositoryType");
            services.AddSingleton(typeof(IBoilerRepository), Type.GetType(typeName));

            object repoInstance = Activator.CreateInstance(Type.GetType(typeName));
            IBoilerRepository repo = repoInstance as IBoilerRepository;
            services.AddInstance(typeof(IBoilerRepository), repo);
            TimerAdapter timer = new TimerAdapter(0, 500);
            BoilerStatusRepository db = new BoilerStatusRepository();
            services.AddInstance(typeof(BoilerMonitor), new BoilerMonitor(repo, timer, db));




            services.AddMvc().AddJsonOptions(options =>
            {
                options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            });


            // Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
            // You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
            // services.AddWebApiConventions();

            return services.BuildServiceProvider();
        }
开发者ID:edwardginhands,项目名称:boil.net,代码行数:37,代码来源:Startup.cs

示例5: ConfigureServices

        // Set up application services
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMvc()
                    .WithControllersAsServices(
                     new[]
                     {
                            typeof(TimeScheduleController).GetTypeInfo().Assembly
                     });

            services.AddTransient<QueryValueService>();

#if DNX451
            // Create the autofac container
            var builder = new ContainerBuilder();

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

            return builder.Build()
                          .Resolve<IServiceProvider>();
#else
            return services.BuildServiceProvider();
#endif
        }
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:27,代码来源:Startup.cs

示例6: ConfigureServices

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddLogging();
            services.AddTransient<TestRunner, TestRunner>();

            return ServiceProvider = services.BuildServiceProvider();
        }
开发者ID:Tasteful,项目名称:bugs,代码行数:7,代码来源:Startup.cs

示例7: ConfigureServices

        public void ConfigureServices(IServiceCollection services)
        {
            var appEnv = services.BuildServiceProvider().GetRequiredService<IApplicationEnvironment>();

            services.AddEntityFramework()
                .AddSqlite()
                .AddDbContext<EMContext>(x => x.UseSqlite("Data source=" + appEnv.ApplicationBasePath + "/Database/EMWeb.db"));

            services.AddIdentity<User, IdentityRole<long>>(x=> {
                x.Password.RequireDigit = false;
                x.Password.RequiredLength = 0;
                x.Password.RequireLowercase = false;
                x.Password.RequireNonLetterOrDigit = false;
                x.Password.RequireUppercase = false;
                x.User.AllowedUserNameCharacters = null;
            })
                .AddEntityFrameworkStores<EMContext,long>()
                .AddDefaultTokenProviders();
            services.AddFileUpload()
                .AddEntityFrameworkStorage<EMContext>();
            services.AddMvc();
            services.AddSmartUser<User,long>();

            
        }
开发者ID:Cream2015,项目名称:EMWeb,代码行数:25,代码来源:Startup.cs

示例8: ConfigureServices

        public void ConfigureServices(IServiceCollection services)
        {
            IConfiguration Configuration;
            services.AddConfiguration(out Configuration);
            var appEnv = services.BuildServiceProvider().GetRequiredService<IApplicationEnvironment>();
            var connStr = "Data source=" + appEnv.ApplicationBasePath + "/" + Configuration["DBFile"] + ";";
            if (connStr.IndexOf('\\') >= 0)
                connStr = connStr.Replace("/", "\\");

            services.AddSmartCookies();

            services.AddJsonLocalization()
                .AddCookieCulture();

            services.AddEntityFramework()
                .AddSqlite()
                .AddDbContext<BlogContext>(options =>
                    options.UseSqlite(connStr));

            services.AddCaching();
            services.AddSession(x => x.IdleTimeout = TimeSpan.FromMinutes(20));

            services.AddMvc()
                .AddTemplate()
                .AddCookieTemplateProvider();
        }
开发者ID:Kagamine,项目名称:YuukoBlog.vNext,代码行数:26,代码来源:Startup.cs

示例9: ConfigureServices

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IInjectedService, InjectedService>();

            services.AddTransient<SimpleDIGrain>();

            return services.BuildServiceProvider();
        }
开发者ID:fgq841103,项目名称:orleans,代码行数:8,代码来源:DependencyInjectionGrainTests.cs

示例10: ConfigureServices

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            _hostingEnv = services.Where(m => m.ServiceType == typeof(IHostingEnvironment) && m.ImplementationInstance != null).Select(m => m.ImplementationInstance).Last() as IHostingEnvironment;
            _startup = new Startup(_hostingEnv);

            ServiceProvider = services.BuildServiceProvider();
            return ServiceProvider;
        }
开发者ID:Pietervdw,项目名称:DBC,代码行数:8,代码来源:Startup.cs

示例11: ConfigureServices

 // This method gets called by the runtime.
 // Use this method to add services to the container.
 public void ConfigureServices(IServiceCollection services)
 {
     services.AddBookStore(Configuration, _loggerFactory);
     var bookDetailLookup = new BookDetailLookup(
         Configuration.GetOrThrow("GOOGLE_PROJECT_ID"), _loggerFactory);
     bookDetailLookup.StartPullLoop(
         services.BuildServiceProvider().GetService<IBookStore>(),
         new CancellationTokenSource().Token);
 }
开发者ID:SurferJeffAtGoogle,项目名称:getting-started-dotnet,代码行数:11,代码来源:Startup.cs

示例12: ConfigureServices

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddOrchardTheming();
            services.AddThemeFolder("Themes");

            services.AddOrchard();

            return services.BuildServiceProvider();
        }
开发者ID:jchenga,项目名称:Orchard2,代码行数:9,代码来源:Startup.cs

示例13: ConfigureDependencyInjection

        public IServiceProvider ConfigureDependencyInjection(IServiceCollection services)
        {
            services.AddScoped<IDatabaseContext>(provider => provider.GetService<DatabaseContext>());
            services.AddScoped<IDbSession, DbSession>();
            services.TryAdd(ServiceDescriptor.Scoped(typeof(IEntityRepository<>), typeof(EntityRepository<>)));
            services.TryAdd(ServiceDescriptor.Scoped(typeof(IBaseService<>), typeof(BaseService<>)));

            return services.BuildServiceProvider();
        }
开发者ID:jruckert,项目名称:ignitedemo2015,代码行数:9,代码来源:Startup.cs

示例14: ConfigureServices

        private IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddOptions();
            services.Configure<ExampleOptions>(GetConfiguration());

            services.AddTransient<IExampleService, ExampleService>(provider => new ExampleService { SomeData = "Hello from Microsoft.Framework.DependencyInjection" });

            return services.BuildServiceProvider();
        }
开发者ID:stephenpope,项目名称:presentation-source,代码行数:9,代码来源:Startup.cs

示例15: ConfigureServices

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services
                .AddWebHost();

            services.AddModuleFolder("~/Core/Orchard.Core");
            services.AddModuleFolder("~/Modules");

            return services.BuildServiceProvider();
        }
开发者ID:Giahim,项目名称:Brochard,代码行数:10,代码来源:Startup.cs


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