本文整理汇总了C#中Microsoft.Framework.DependencyInjection.ServiceCollection.TryAdd方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.TryAdd方法的具体用法?C# ServiceCollection.TryAdd怎么用?C# ServiceCollection.TryAdd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Framework.DependencyInjection.ServiceCollection
的用法示例。
在下文中一共展示了ServiceCollection.TryAdd方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryAddNoOpFailsIfExists
public void TryAddNoOpFailsIfExists(Action<IServiceCollection> addAction)
{
// Arrange
var collection = new ServiceCollection();
addAction(collection);
var d = ServiceDescriptor.Transient<IFakeService, FakeService>();
// Act
collection.TryAdd(d);
// Assert
var descriptor = Assert.Single(collection);
Assert.Equal(typeof(IFakeService), descriptor.ServiceType);
Assert.Same(_instance, descriptor.ImplementationInstance);
Assert.Equal(ServiceLifetime.Singleton, descriptor.Lifetime);
}
示例2: TryAddWithEnumerableReturnsTrueIfAnyAdded
public void TryAddWithEnumerableReturnsTrueIfAnyAdded()
{
// Arrange
var collection = new ServiceCollection();
// Act
var ds = new ServiceDescriptor[] {
ServiceDescriptor.Transient<IFakeService, FakeService>(),
ServiceDescriptor.Transient<IFakeService, FakeService>()
};
// Assert
Assert.True(collection.TryAdd(ds));
var descriptor = Assert.Single(collection);
Assert.Equal(typeof(IFakeService), descriptor.ServiceType);
Assert.Null(descriptor.ImplementationInstance);
Assert.Equal(ServiceLifetime.Transient, descriptor.Lifetime);
}
示例3: TryAddIfMissingActuallyAdds
public void TryAddIfMissingActuallyAdds()
{
// Arrange
var collection = new ServiceCollection();
var d = ServiceDescriptor.Transient<IFakeService, FakeService>();
// Act
collection.TryAdd(d);
// Assert
var descriptor = Assert.Single(collection);
Assert.Equal(typeof(IFakeService), descriptor.ServiceType);
Assert.Null(descriptor.ImplementationInstance);
Assert.Equal(ServiceLifetime.Transient, descriptor.Lifetime);
}
示例4: GetDefaultServices
public static IServiceCollection GetDefaultServices()
{
var services = new ServiceCollection();
// Options and core services.
services.AddTransient<IConfigureOptions<MvcOptions>, MvcOptionsSetup>();
services.AddTransient<IConfigureOptions<RazorViewEngineOptions>, RazorViewEngineOptionsSetup>();
// TryAdd() so functional tests can override this particular service. Test setup runs before this method.
services.TryAdd(ServiceDescriptor.Transient<IAssemblyProvider, DefaultAssemblyProvider>());
services.AddTransient<MvcMarkerService, MvcMarkerService>();
services.AddSingleton<ITypeActivatorCache, DefaultTypeActivatorCache>();
services.AddScoped(typeof(IScopedInstance<>), typeof(ScopedInstance<>));
// Core action discovery, filters and action execution.
// These are consumed only when creating action descriptors, then they can be de-allocated
services.AddTransient<IControllerTypeProvider, DefaultControllerTypeProvider>();
services.AddTransient<IControllerModelBuilder, DefaultControllerModelBuilder>();
services.AddTransient<IActionModelBuilder, DefaultActionModelBuilder>();
// This has a cache, so it needs to be a singleton
services.AddSingleton<IControllerFactory, DefaultControllerFactory>();
services.AddTransient<IControllerActivator, DefaultControllerActivator>();
// This accesses per-reqest services
services.AddTransient<IActionInvokerFactory, ActionInvokerFactory>();
// This provider needs access to the per-request services, but might be used many times for a given
// request.
services.AddTransient<IActionConstraintProvider, DefaultActionConstraintProvider>();
services.AddSingleton<IActionSelectorDecisionTreeProvider, ActionSelectorDecisionTreeProvider>();
services.AddSingleton<IActionSelector, DefaultActionSelector>();
services.AddTransient<IControllerActionArgumentBinder, DefaultControllerActionArgumentBinder>();
services.AddTransient<IObjectModelValidator, DefaultObjectValidator>();
services.AddTransient<IActionDescriptorProvider, ControllerActionDescriptorProvider>();
services.AddTransient<IActionInvokerProvider, ControllerActionInvokerProvider>();
services.AddSingleton<IActionDescriptorsCollectionProvider, DefaultActionDescriptorsCollectionProvider>();
// The IGlobalFilterProvider is used to build the action descriptors (likely once) and so should
// remain transient to avoid keeping it in memory.
services.AddTransient<IGlobalFilterProvider, DefaultGlobalFilterProvider>();
services.AddTransient<IFilterProvider, DefaultFilterProvider>();
services.AddTransient<FormatFilter, FormatFilter>();
services.AddTransient<CorsAuthorizationFilter, CorsAuthorizationFilter>();
// Dataflow - ModelBinding, Validation and Formatting
//
// The DefaultModelMetadataProvider does significant caching and should be a singleton.
services.AddSingleton<ModelBinding.IModelMetadataProvider, DefaultModelMetadataProvider>();
services.AddTransient<ModelBinding.Metadata.ICompositeMetadataDetailsProvider>(serviceProvider =>
{
var options = serviceProvider.GetRequiredService<IOptions<MvcOptions>>().Options;
return new DefaultCompositeMetadataDetailsProvider(options.ModelMetadataDetailsProviders);
});
services.AddTransient<IInputFormatterSelector, DefaultInputFormatterSelector>();
services.AddScoped<IInputFormattersProvider, DefaultInputFormattersProvider>();
services.AddTransient<IModelBinderProvider, DefaultModelBindersProvider>();
services.AddTransient<IValueProviderFactoryProvider, DefaultValueProviderFactoryProvider>();
services.AddTransient<IOutputFormattersProvider, DefaultOutputFormattersProvider>();
services.AddInstance(new JsonOutputFormatter());
services.AddTransient<IModelValidatorProviderProvider, DefaultModelValidatorProviderProvider>();
services.AddTransient<IValidationExcludeFiltersProvider, DefaultValidationExcludeFiltersProvider>();
// Razor, Views and runtime compilation
// The provider is inexpensive to initialize and provides ViewEngines that may require request
// specific services.
services.AddScoped<ICompositeViewEngine, CompositeViewEngine>();
services.AddTransient<IViewEngineProvider, DefaultViewEngineProvider>();
// Transient since the IViewLocationExpanders returned by the instance is cached by view engines.
services.AddTransient<IViewLocationExpanderProvider, DefaultViewLocationExpanderProvider>();
// Caches view locations that are valid for the lifetime of the application.
services.AddSingleton<IViewLocationCache, DefaultViewLocationCache>();
services.AddSingleton<ICodeTreeCache>(serviceProvider =>
{
var cachedFileProvider = serviceProvider.GetRequiredService<IOptions<RazorViewEngineOptions>>();
return new DefaultCodeTreeCache(cachedFileProvider.Options.FileProvider);
});
// The host is designed to be discarded after consumption and is very inexpensive to initialize.
services.AddTransient<IMvcRazorHost, MvcRazorHost>();
// Caches compilation artifacts across the lifetime of the application.
services.AddSingleton<ICompilerCache, CompilerCache>();
// This caches compilation related details that are valid across the lifetime of the application
// and is required to be a singleton.
services.AddSingleton<ICompilationService, RoslynCompilationService>();
//.........这里部分代码省略.........