本文整理汇总了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();
}
}
示例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();
}
}
示例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);
}
示例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();
}
示例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
}
示例6: ConfigureServices
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddLogging();
services.AddTransient<TestRunner, TestRunner>();
return ServiceProvider = services.BuildServiceProvider();
}
示例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>();
}
示例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();
}
示例9: ConfigureServices
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IInjectedService, InjectedService>();
services.AddTransient<SimpleDIGrain>();
return services.BuildServiceProvider();
}
示例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;
}
示例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);
}
示例12: ConfigureServices
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddOrchardTheming();
services.AddThemeFolder("Themes");
services.AddOrchard();
return services.BuildServiceProvider();
}
示例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();
}
示例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();
}
示例15: ConfigureServices
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services
.AddWebHost();
services.AddModuleFolder("~/Core/Orchard.Core");
services.AddModuleFolder("~/Modules");
return services.BuildServiceProvider();
}