本文整理汇总了C#中IServiceCollection.AddInstance方法的典型用法代码示例。如果您正苦于以下问题:C# IServiceCollection.AddInstance方法的具体用法?C# IServiceCollection.AddInstance怎么用?C# IServiceCollection.AddInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IServiceCollection
的用法示例。
在下文中一共展示了IServiceCollection.AddInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
var builder = new ConfigurationBuilder()
.SetBasePath(this.appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddEnvironmentVariables();
var configRoot = builder.Build();
this.env.Initialize(this.appEnv.ApplicationBasePath, configRoot);
services.AddInstance(configRoot);
services.AddInstance(new Configuration(configRoot));
DatabaseConfiguration.ConfigureIdentity(services, this.env);
DatabaseConfiguration.ConfigureEntityFramework(services, configRoot, this.env, IsWindows);
this.AddMvcServiceSetup(services);
services.AddCaching();
services.AddSession();
services.AddLogging();
services.AddAntiforgery();
services.AddSingleton<IAntiforgeryTokenStore, AntiforgeryTokenStore>();
services.AddTransient<ISeedData, SeedData>();
services.AddScoped<ViewModelValidator>();
RepositoryConfiguration.ConfigureRepositories(services);
}
示例2: 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();
}
示例3: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
// register services for dependency injection
var repository = new InMemoryConversionRepository();
services.AddInstance<IConversionRepository>(repository);
services.AddInstance<IConversionScheduler>(new XUnit2NUnitConverter(repository));
services.AddMvc();
}
示例4: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
Workspace = CreateWorkspace();
services.AddMvc();
services.Configure<MvcOptions>(opt =>
{
opt.Conventions.Add(new FromBodyApplicationModelConvention());
opt.Filters.Add(new UpdateBufferFilter(Workspace));
});
// Add the omnisharp workspace to the container
services.AddInstance(Workspace);
// Caching
services.AddSingleton<IMemoryCache, MemoryCache>();
services.AddSingleton<IMetadataFileReferenceCache, MetadataFileReferenceCache>();
// Add the project systems
services.AddInstance(new DnxContext());
services.AddInstance(new MSBuildContext());
services.AddInstance(new ScriptCs.ScriptCsContext());
services.AddSingleton<IProjectSystem, DnxProjectSystem>();
services.AddSingleton<IProjectSystem, MSBuildProjectSystem>();
#if DNX451
services.AddSingleton<IProjectSystem, ScriptCs.ScriptCsProjectSystem>();
#endif
// Add the file watcher
services.AddSingleton<IFileSystemWatcher, ManualFileSystemWatcher>();
// Add test command providers
services.AddSingleton<ITestCommandProvider, DnxTestCommandProvider>();
#if DNX451
//TODO Do roslyn code actions run on Core CLR?
services.AddSingleton<ICodeActionProvider, RoslynCodeActionProvider>();
services.AddSingleton<ICodeActionProvider, NRefactoryCodeActionProvider>();
#endif
if (Program.Environment.TransportType == TransportType.Stdio)
{
services.AddSingleton<IEventEmitter, StdioEventEmitter>();
}
else
{
services.AddSingleton<IEventEmitter, NullEventEmitter>();
}
services.AddSingleton<ProjectEventForwarder, ProjectEventForwarder>();
// Setup the options from configuration
services.Configure<OmniSharpOptions>(Configuration);
}
示例5: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddRouting();
services.AddLogging();
services.AddAuthorization();
services.AddInstance<IControllerActivator>(new SimpleInjectorControllerActivator(container));
services.AddInstance<IViewComponentInvokerFactory>(new SimpleInjectorViewComponentInvokerFactory(container));
}
示例6: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddInstance<IControllerActivator>(new SimpleInjectorControllerActivator(container));
services.AddInstance<IViewComponentInvokerFactory>(new SimpleInjectorViewComponentInvokerFactory(container));
var assembliesToInspect = GetAssemblies();
services.AddLifecycleCommands(assembliesToInspect);
services.AddMvc();
services.AddCaching();
services.AddSession(o =>
{
o.IdleTimeout = TimeSpan.FromSeconds(3600);
});
}
示例7: ConfigureServices
/// <summary>
/// Configure Dependencies Injection
/// </summary>
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddInstance(Configuration);
// initialize youtube service
services.AddTransient(CreateYoutubeService);
services.AddInstance(GetMongoDatabase());
services.AddTransient<IRepository<Video>>(
provider => new MongoRepository<Video>(provider.GetService<IMongoDatabase>())
);
}
示例8: ConfigureServices
// Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().Configure<MvcOptions>(opt => {
//change json output formatting to indented
foreach (JsonOutputFormatter of in opt.OutputFormatters.OfType<JsonOutputFormatter>()) {
of.SerializerSettings.Formatting = Formatting.Indented;
}
});
//configure app
App.Configure(configuration);
//configure DI
services.AddInstance(typeof(IFeed), App.twitterFeed);
services.AddInstance(typeof(IAggregationService), AggregationService.instance);
}
示例9: ConfigureServices
// This method gets called by a runtime.
// Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.Configure<MvcOptions>(options =>
{
var jsonFormatter = (JsonOutputFormatter)(options.OutputFormatters
.First(formatter => formatter.Instance is JsonOutputFormatter)
.Instance);
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
//options.Filters.Add(new RequireHttpsAttribute());
});
services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();
services.AddTransient<ILinkHelper, LinkHelper>();
services.AddScoped<ISecurityHelper, SecurityHelper>();
services.AddSingleton <IWebJobController, WebJobController>();
services.AddSingleton<IVolatileStorageController, VolatileStorageController>();
services.AddSingleton<IGraphAPIProvider, GraphAPIProvider>();
services.AddInstance(Configuration);
services.AddDocumentDbRepositories(Configuration);
services.AddKeyVaultRepositories(Configuration);
}
示例10: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//load key from file
RSAParameters keyParams = SecurityRSAUtils.GetKeyParameters(@"C:\_Projects\auth.spear.roimantra.com\src\auth.spear.roimantra.com\SecurityKey\jwt_private_security_key.txt");
_securityKey = new RsaSecurityKey(keyParams);
_tokenOptions = new SecurityTokenAuthOptions()
{
Audience = TokenAudience,
Issuer = TokenIssuer,
SigningCredentials = new SigningCredentials(_securityKey, SecurityAlgorithms.RsaSha256Signature)
};
// Save the token options into an instance so they're accessible to the
// controller.
services.AddInstance<SecurityTokenAuthOptions>(_tokenOptions);
// Enable the use of an [Authorize("Bearer")] attribute on methods and classes to protect.
services.AddAuthorization(auth =>
{
auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser().Build());
});
services.AddMvc();
}
示例11: ConfigureServices
// Set up application services
public void ConfigureServices(IServiceCollection services)
{
// Add MVC services to the services container
services.AddMvc();
services.AddInstance(new MyService());
services.AddScoped<ViewService, ViewService>();
}
示例12: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddScoped<SearchRepository, SearchRepository>();
services.AddInstance(_configuration);
}
示例13: ConfigureServices
public void ConfigureServices(IServiceCollection svcs)
{
svcs.AddInstance(_config);
if (_env.IsDevelopment())
{
svcs.AddTransient<IMailService, LoggingMailService>();
}
else
{
svcs.AddTransient<IMailService, MailService>();
}
svcs.AddEntityFramework()
.AddSqlServer()
.AddDbContext<WilderContext>();
svcs.AddScoped<IWilderRepository, WilderRepository>();
svcs.AddMvc()
.AddJsonOptions(opts =>
{
opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
}
示例14: ConfigureServices
// This method gets called by a runtime.
// Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
services.AddInstance<IConfiguration>(Configuration);
services.AddCustomBindings(Configuration);
services.AddMvc().Configure<MvcOptions>(options =>
{
options.OutputFormatters.OfType<JsonOutputFormatter>()
.First()
.SerializerSettings
.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
// CORS
services.AddCors();
var policy = new Microsoft.AspNet.Cors.Core.CorsPolicy();
policy.Headers.Add("*");
policy.Methods.Add("*");
policy.Origins.Add("*");
policy.SupportsCredentials = true;
services.ConfigureCors(x => x.AddPolicy("mypolicy", policy));
// 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();
}
示例15: HostingEngine
public HostingEngine(
IServiceCollection appServices,
IStartupLoader startupLoader,
IConfiguration config,
bool captureStartupErrors)
{
if (appServices == null)
{
throw new ArgumentNullException(nameof(appServices));
}
if (startupLoader == null)
{
throw new ArgumentNullException(nameof(startupLoader));
}
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
_config = config;
_applicationServiceCollection = appServices;
_startupLoader = startupLoader;
_captureStartupErrors = captureStartupErrors;
_applicationLifetime = new ApplicationLifetime();
_applicationServiceCollection.AddInstance<IApplicationLifetime>(_applicationLifetime);
}