simple-injector

Cannot resolve ILogger<T> Simple Injector ASP.NET Core 2.0

假装没事ソ 提交于 2020-02-27 06:28:25
问题 We use Simple Injector in ASP.NET Core application. Recently we've decided to use Serilog for logging purposes. Configuration was done in Program.cs as stated in their documentation. Then, in order to make Simple Injector able to resolve ILoggerFactory I did something like this: public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(); // As per my understanding, we force SimpleInjector to use //

How to inject null for unregistered services

泪湿孤枕 提交于 2020-02-06 06:06:12
问题 I have a decorator and an associated service that looks like this: public interface IAdditionalDataService<TResult, TModel> { TResult PopulateAdditionalData(TResult model, params Expression<Func<TModel, object>>[] properties); } public class AdditionalDataDecorator<TQuery, TResult, TModel> : IQueryHandler<TQuery, TResult> where TQuery : QueryBase<TResult, TModel>, IQuery<TResult> where TModel : ModelBase { private readonly IQueryHandler<TQuery, TResult> _decorated; private readonly

How to inject null for unregistered services

家住魔仙堡 提交于 2020-02-06 06:06:05
问题 I have a decorator and an associated service that looks like this: public interface IAdditionalDataService<TResult, TModel> { TResult PopulateAdditionalData(TResult model, params Expression<Func<TModel, object>>[] properties); } public class AdditionalDataDecorator<TQuery, TResult, TModel> : IQueryHandler<TQuery, TResult> where TQuery : QueryBase<TResult, TModel>, IQuery<TResult> where TModel : ModelBase { private readonly IQueryHandler<TQuery, TResult> _decorated; private readonly

Simple Injector: Injecting a property in a base class

百般思念 提交于 2020-02-01 05:31:25
问题 For several weeks I've been using the Simple Injector dependency injection container, with great success. I love the easy by which I can configure it. But now I have a design that I don't know how to configure. I have a base class where many types from derive, and I want to inject a dependency into a property of the base class, but without having to configure that for every derived class. I tried to do this with attributes, but Simple Injector does not support attributes. Here is a trimmed

Registering a type with multiple constructors and string dependency in Simple Injector

断了今生、忘了曾经 提交于 2020-02-01 00:37:36
问题 I'm trying to figure out how to use Simple Injector, I've used it around the project with no problems registering simple services and their components. However, I wanted to use dependency injector when having a component with more than two constructors that implements an interface. public DAL: IDAL { private Logger logger; string _dbInstance; public DAL() { logger = new Logger(); } public DAL(string databaseInstance) { logger = new Logger(); _dbInstance = databaseInstance; } } Here is how I'm

How can i register a type with its interfaces as singleton using simple injector?

泄露秘密 提交于 2020-01-24 09:07:13
问题 I have models like below: public interface IUserLookupService { Guid[] GetDirectChilds(Guid userId); Guid[] GetAllChilds(Guid userId); Guid GetDepartment(Guid userId); } public interface ICanRefreshCache{ void Refresh(); } public class XHandler : IEventHandler<UserNameChanged> { ... } public class YHandler : IEventHandler<TeamCreated> { ... } public class CachedUserLookupService : IUserLookupService, ICanRefreshCache, IEventHandler<UserNameChanged> { private Func<ISession> _sessionFactory;

How can i register a type with its interfaces as singleton using simple injector?

こ雲淡風輕ζ 提交于 2020-01-24 09:05:47
问题 I have models like below: public interface IUserLookupService { Guid[] GetDirectChilds(Guid userId); Guid[] GetAllChilds(Guid userId); Guid GetDepartment(Guid userId); } public interface ICanRefreshCache{ void Refresh(); } public class XHandler : IEventHandler<UserNameChanged> { ... } public class YHandler : IEventHandler<TeamCreated> { ... } public class CachedUserLookupService : IUserLookupService, ICanRefreshCache, IEventHandler<UserNameChanged> { private Func<ISession> _sessionFactory;

How to get service from ValidationContext using Simple Injector?

送分小仙女□ 提交于 2020-01-23 11:07:43
问题 In my Asp.Net MVC Core project I use SimpleInjector as IoC. I use it because of possibility of registering open generics. In some of my viewmodels I implement IValidatableObject . public class MyViewmodel: IValidatableObject { public string SomeProperty { get;set; } //... public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { //... IMyService service = validationContext.GetService(typeof(IMyService)) as IMyService; } } And method GetService returns null because

How to get service from ValidationContext using Simple Injector?

Deadly 提交于 2020-01-23 11:07:06
问题 In my Asp.Net MVC Core project I use SimpleInjector as IoC. I use it because of possibility of registering open generics. In some of my viewmodels I implement IValidatableObject . public class MyViewmodel: IValidatableObject { public string SomeProperty { get;set; } //... public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { //... IMyService service = validationContext.GetService(typeof(IMyService)) as IMyService; } } And method GetService returns null because

Prevent Simple Injector to throw an exception when resolving an unregistered service

旧时模样 提交于 2020-01-22 15:43:05
问题 I was wondering if Simple Injector has an option to stop throwing exceptions whenever GetInstance(Of TService) returns Nothing ? It appears to be throwing them now because I have two requests to get an instance, it's not there, and it throws the exception. Is there a way to prevent the default behavior, a setting somewhere, or something else? 回答1: There absolutely is a simple way of doing this. The SimpleInjector.Container implements System.IServiceProvider, which defines an object GetService