Esempio n. 1
0
        static void Main(string[] args)
        {
            //创建一个容器
            IServiceContainer services = new ServiceContainer();

            services.Configure(config =>
            {
                config.Interceptors.AddTyped <SampleInterceptor>();
            });

            services.AddType <ISampleService, SampleService>();
            services.AddType <ISampleRepository, SampleRepository>();

            //使用类型注册服务
            services.AddType <ILogger, Logger>();
            //使用实例注册服务,服务的生命周期限定为单例
            services.AddInstance <ITaskService>(new TaskService());
            //使用委托工厂注册服务
            services.AddDelegate <ITaskService, TaskService>(resolver => new TaskService());

            //创建服务解析器
            IServiceResolver serviceResolver = services.Build();

            //解析单个服务
            ISampleService sampleService = serviceResolver.Resolve <ISampleService>();

            //解析单个服务,并且验证是否为null,为null则抛出异常
            ISampleService sampleServiceRequired = serviceResolver.ResolveRequired <ISampleService>();

            //解析服务集合,如果未注册,则为空集合
            IEnumerable <ISampleService> sampleServices = serviceResolver.ResolveMany <ISampleService>();
        }
Esempio n. 2
0
 /// <inheritdoc />
 public IEnumerable <object> ResolveMany(Type serviceType) => _resolver.ResolveMany(serviceType);
 /// <summary>
 /// Create a new instance of <see cref="AspectCoreLogServiceProvider"/>
 /// </summary>
 /// <param name="provider"></param>
 /// <param name="loggingConfiguration"></param>
 public AspectCoreLogServiceProvider(IServiceResolver provider, LoggingConfiguration loggingConfiguration)
 {
     _provider = provider ?? throw new ArgumentNullException(nameof(provider));
     _logPayloadClientProviders = _provider.ResolveMany <ILogPayloadClientProvider>() ?? Enumerable.Empty <ILogPayloadClientProvider>();
     _loggingConfiguration      = loggingConfiguration ?? throw new ArgumentNullException(nameof(loggingConfiguration));
 }