/// <summary> /// 创建单例类型的映射并尝试到服务映射集合中 /// </summary> /// <param name="collection">服务映射集合</param> /// <param name="serviceType">服务类型</param> /// <param name="implementationType">服务实现类型</param> /// <returns></returns> public static IServiceCollection TryAddSingleton(this IServiceCollection collection, Type serviceType, Type implementationType) { ServiceDescriptor descriptor = ServiceDescriptor.Singleton(serviceType, implementationType); return(collection.TryAdd(descriptor)); }
/// <summary> /// 创建单例类型的映射并尝试到服务映射集合中 /// </summary> /// <param name="collection">服务映射集合</param> /// <param name="serviceType">服务类型</param> /// <param name="factory">服务实现类型实例工厂</param> /// <returns></returns> public static IServiceCollection TryAddSingleton(this IServiceCollection collection, Type serviceType, Func <IServiceProvider, object> factory) { ServiceDescriptor descriptor = ServiceDescriptor.Singleton(serviceType, factory); return(collection.TryAdd(descriptor)); }
/// <summary> /// 创建范围类型的映射并尝试到服务映射集合中 /// </summary> /// <param name="collection">服务映射集合</param> /// <param name="serviceType">服务类型</param> /// <returns></returns> public static IServiceCollection TryAddScoped(this IServiceCollection collection, Type serviceType) { ServiceDescriptor descriptor = ServiceDescriptor.Scoped(serviceType, serviceType); return(collection.TryAdd(descriptor)); }
/// <summary> /// 将映射描述直接添加到服务映射集合中 /// </summary> /// <param name="collection">服务映射集合</param> /// <param name="descriptor">服务映射信息</param> /// <returns></returns> public static IServiceCollection AddDescriptor(this IServiceCollection collection, ServiceDescriptor descriptor) { Type implementationType = descriptor.GetImplementationType(); if (implementationType == typeof(object) || implementationType == descriptor.ServiceType) { throw new InvalidOperationException(Resources.Ioc_TryAddIndistinguishableTypeToEnumerable.FormatWith(implementationType, descriptor.ServiceType)); } if (!collection.Any(m => m.ServiceType == descriptor.ServiceType && m.GetImplementationType() == implementationType)) { collection.Add(descriptor); } return(collection); }
private static IServiceCollection Add(IServiceCollection collection, Type serviceType, Func <IServiceProvider, object> factory, LifetimeStyle lifetime) { ServiceDescriptor descriptor = new ServiceDescriptor(serviceType, factory, lifetime); return(collection.TryAdd(descriptor)); }
/// <summary> /// 注册指定生命周期类型的映射信息 /// </summary> /// <param name="collection">服务映射信息集合</param> /// <param name="serviceType">服务类型</param> /// <param name="implementationType">服务实现类型</param> /// <param name="lifetime">生命周期类型</param> /// <returns></returns> public static IServiceCollection Add(this IServiceCollection collection, Type serviceType, Type implementationType, LifetimeStyle lifetime) { ServiceDescriptor descriptor = new ServiceDescriptor(serviceType, implementationType, lifetime); return(collection.TryAdd(descriptor)); }
/// <summary> /// 注册单例生命周期的实例映射信息 /// </summary> /// <param name="collection">服务映射信息集合</param> /// <param name="serviceType">服务类型</param> /// <param name="instance">服务实现类型实例</param> /// <returns>服务映射信息集合</returns> public static IServiceCollection AddInstance(this IServiceCollection collection, Type serviceType, object instance) { ServiceDescriptor descriptor = new ServiceDescriptor(serviceType, instance); return(collection.TryAdd(descriptor)); }