本文整理汇总了C#中IServiceCollection.Add方法的典型用法代码示例。如果您正苦于以下问题:C# IServiceCollection.Add方法的具体用法?C# IServiceCollection.Add怎么用?C# IServiceCollection.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IServiceCollection
的用法示例。
在下文中一共展示了IServiceCollection.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddLogging();
services.Add(new ServiceDescriptor(typeof(ISeriesLogic), typeof(SeriesLogic.SeriesLogic), ServiceLifetime.Transient));
services.Add(new ServiceDescriptor(typeof(ISeriesCache), typeof(InMemoryCache), ServiceLifetime.Singleton));
services.Add(new ServiceDescriptor(typeof(ISeriesEvaluator), typeof(FibonacciSeriesEvaluator), ServiceLifetime.Transient));
services.Add(new ServiceDescriptor(typeof(ISeriesEvaluator), typeof(PrimeSeriesEvaluator), ServiceLifetime.Transient));
}
示例2: Configure
public void Configure(IServiceCollection serviceCollection)
{
// MVC is already registering IMemoryCache as host singleton. We are registering it again
// in this module so that there is one instance for each tenant.
serviceCollection.Add(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());
// LocalCache is registered as transient as its implementation resolves IMemoryCache, thus
// there is no state to keep in its instance.
serviceCollection.Add(ServiceDescriptor.Transient<IDistributedCache, MemoryDistributedCache>());
}
示例3: ConfigureServices
// This method gets called by the runtime.
public void ConfigureServices(IServiceCollection services)
{
// Add MVC services to the services container.
services.AddMvc();
var d = new ServiceDescriptor(typeof(IUnitOfWork), new UnitOfWork());
services.Add(d);
}
示例4: Register
public void Register(IServiceCollection serviceCollection)
{
foreach (var service in _serviceCollection)
{
serviceCollection.Add(service);
}
}
示例5: Configure
public void Configure(IServiceCollection serviceCollection)
{
// MVC is already registering IMemoryCache. Any module that would add another implementation
// would take over the default one as the last registered service will be resolved.
// serviceCollection.Add(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());
// LocalCache is registered as Transient as its implementation resolves IMemoryCache, thus
// there is no state to keep in its instance.
serviceCollection.Add(ServiceDescriptor.Transient<IDistributedCache, LocalCache>());
}
示例6: MergeServiceDescriptions
private static void MergeServiceDescriptions(IServiceCollection serviceCollection, IEnumerable<ServiceDescriptor> serviceDescriptors)
{
var excludedServiceDescriptors = serviceCollection.Where(service => serviceDescriptors.Contains(service) == false);
foreach (var descriptor in excludedServiceDescriptors)
{
serviceCollection.Remove(descriptor);
}
foreach (var descriptor in serviceDescriptors)
{
serviceCollection.Add(descriptor);
}
}
示例7: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//This can be injected to any service, controller...
services.Add(
new ServiceDescriptor(
typeof (IOptions<RedisCacheOptions>),
Configuration.Get<RedisCacheOptions>("RedisCacheOptions")
)
);
//At the moment the redis cache implements the wrong interface, that's why the OwnRedisCache
services.AddSingleton<IDistributedCache, OwnRedisCache>();
services.AddSession(o => { o.IdleTimeout = TimeSpan.FromMinutes(15); });
services.AddMvc();
}
示例8: RegisterAssembly
public void RegisterAssembly(IServiceCollection services, AssemblyName assemblyName, string path = "")
{
var loadContext = PlatformServices.Default.AssemblyLoadContextAccessor.Default;
var loader = new DirectoryLoader(path, loadContext);
var assembly = loader.Load(assemblyName);
foreach (var type in assembly.DefinedTypes)
{
var dependencyAttributes = type.GetCustomAttributes<DependencyAttribute>();
// Each dependency can be registered as various types
foreach (var serviceDescriptor in dependencyAttributes.Select(dependencyAttribute => dependencyAttribute.BuiildServiceDescriptor(type)))
{
services.Add(serviceDescriptor);
}
}
}
示例9: WireUpDbContexts
private static void WireUpDbContexts(IServiceCollection services, IConfiguration configuration)
{
var cmsConnString = configuration["Data:cmsconnection:ConnectionString"];
var authConnString = configuration["Data:authconnection:ConnectionString"];
var connectionStrings = new Dictionary<Type, string>
{
{typeof (AuthDbContext), authConnString},
{typeof (ElmahDbContext), cmsConnString},
{typeof (ComponentsDbContext), cmsConnString}
};
var serviceDescriptor = new ServiceDescriptor(typeof(IDbContextFactory), new DbContextFactory(connectionStrings));
services.Add(serviceDescriptor);
services.AddSingleton<IDbContextScopeFactory, DbContextScopeFactory>();
services.AddSingleton<IAmbientDbContextLocator, AmbientDbContextLocator>();
}
示例10: Populate
public void Populate(IServiceCollection services)
{
foreach (var type in Types)
{
var typeInfo = type.GetTypeInfo();
var attribute = typeInfo.GetCustomAttribute<ServiceDescriptorAttribute>();
if (attribute == null)
{
continue;
}
var serviceType = attribute.ServiceType ?? type;
var descriptor = new ServiceDescriptor(serviceType, type, attribute.Lifetime);
services.Add(descriptor);
}
}
示例11: Register
internal static void Register(IServiceCollection serviceCollection)
{
serviceCollection.Add(ServiceDescriptor.Singleton(new DataActionTokenResolver()));
}
示例12: TryAddMultiRegistrationService
// Adds a service if the service type and implementation type hasn't been added yet. This is needed for
// services like IConfigureOptions<MvcOptions> or IApplicationModelProvider where you need the ability
// to register multiple implementation types for the same service type.
private static bool TryAddMultiRegistrationService(IServiceCollection services, ServiceDescriptor descriptor)
{
// This can't work when registering a factory or instance, you have to register a type.
// Additionally, if any existing registrations use a factory or instance, we can't check those, but we don't
// assert that because it might be added by user-code.
Debug.Assert(descriptor.ImplementationType != null);
if (services.Any(d =>
d.ServiceType == descriptor.ServiceType &&
d.ImplementationType == descriptor.ImplementationType))
{
return false;
}
services.Add(descriptor);
return true;
}
示例13: AddTypeWithInterfaces
/// <summary>
/// 以类型实现的接口进行服务添加,需排除
/// <see cref="ITransientDependency"/>、
/// <see cref="IScopeDependency"/>、
/// <see cref="ISingletonDependency"/>、
/// <see cref="IDependency"/>、
/// <see cref="IDisposable"/>等非业务接口,如无接口则注册自身
/// </summary>
/// <param name="services">服务映射信息集合</param>
/// <param name="implementationTypes">要注册的实现类型集合</param>
/// <param name="lifetime">注册的生命周期类型</param>
protected virtual void AddTypeWithInterfaces(IServiceCollection services, Type[] implementationTypes, LifetimeStyle lifetime)
{
foreach (Type implementationType in implementationTypes)
{
if (implementationType.IsAbstract || implementationType.IsInterface)
{
continue;
}
Type[] interfaceTypes = GetImplementedInterfaces(implementationType);
if (interfaceTypes.Length == 0)
{
services.Add(implementationType, implementationType, lifetime);
continue;
}
foreach (Type interfaceType in interfaceTypes)
{
services.Add(interfaceType, implementationType, lifetime);
}
}
}
示例14: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var descriptor = new ServiceDescriptor(typeof(IHitCounterService), new HitCounterService(_rootPath));
services.Add(descriptor);
}
示例15: ConfigureServices
// This method gets called by the runtime.
public void ConfigureServices(IServiceCollection services)
{
// Add EF services to the services container.
//services.AddEntityFramework(Configuration)
// .AddSqlServer()
// .AddDbContext<MoBContext>();
//// Add Identity services to the services container.
//services.AddIdentity<ApplicationUser, IdentityRole>(Configuration)
// .AddEntityFrameworkStores<MoBContext>();
var d = new ServiceDescriptor(typeof(IUnitOfWork), new UnitOfWork());
services.Add(d);
// Add MVC services to the services container.
services.AddMvc();
}