simple-injector

Resolving a class with a custom parameter in Simple Injector

与世无争的帅哥 提交于 2019-12-10 10:22:41
问题 I'm creating a WPF MVVM application using Simple Injector as DI container. Now I'm having some issues when I'm trying to resolve a view from Simple Injector, because I'm in need of passing a parameter into my constructor at construction time (not when registering the view to the container, thus this is not applicable: Simple Injector pass values into constructor). What I'm after is something like this: var item = container.GetInstance<MyType>(myParameter); I've read several places that this

Using SimpleInjector Is there a way to replicate RegisterWebApiRequest<T>() with .net 4/webapi 1?

谁说胖子不能爱 提交于 2019-12-10 06:15:00
问题 Following up on this question. I'm working through example of using SimpleInjector and WebAPI. Unfortunately, where I want to utilize WebAPI KB2568167 and KB2915689 prevent me from upgrading to .net 4.5. So I'm stuck using .net 4.0 & WebAPI v1 (4.0.30506.0) at the moment. Is there a way to replicate the RegisterWebApiRequest<T>() scoping with the older version of WebAPI? While I the nu-get packages only contain .net 4.5 versions, I was able to download the code and get a framework 4.0 compile

Simple Injector - No parameterless constructor defined for this object

瘦欲@ 提交于 2019-12-10 02:11:31
问题 I have a new MVC Web Project which i am usin MVC and WebApi in. I have setup Simple Injector (version 2.5.2 from nuGet) using the following code in my global file // Register Injectors SimpleInjectorConfig.Register(); In my SimpleInjectorConfig.cs file i have public class SimpleInjectorConfig { public static void Register() { // Create the container as usual. Container container = new Container(); // services container.Register<IService, MyService>(); // data container.Register<IRepository,

Simple injector open generic decorators

人走茶凉 提交于 2019-12-09 14:00:25
问题 I am trying to make use of some of the nice features in simple injector. I am currently having problems with the decorators, they are not getting hit when I expect them too. I am registering them like this: container.RegisterManyForOpenGeneric( typeof(ICommandHandler<>), AppDomain.CurrentDomain.GetAssemblies()); container.RegisterDecorator( typeof(ICommandHandler<>), typeof(CreateValidFriendlyUrlCommandHandler<>), context => context.ServiceType == typeof(ICommandHandler<CreateProductCommand>)

Register decorator with another dependency of same generic type

不问归期 提交于 2019-12-09 12:35:35
问题 I think I have stumbled upon a quirk in Simple Injector's RegisterDecorator() . It occurs even in the most recent 2.5.0. I have a situation where I want to decorate a closed generic type, for example ICommandHandler<MessageCommand> , with a decorator that takes (via constructor injection) an inner handler of type ICommandHandler<MessageCommand> , but also another type of handler, say ICommandHandler<LogCommand> . Even though these command handler types are distinct, SimpleInjector seems to

Registering NLog ILogger with Simple Injector

流过昼夜 提交于 2019-12-09 09:25:12
问题 Is there any way I can get the context so I can retrieve the loggerName and use LogManager.GetLogger(loggerName) instead of LogManager.GetCurrentClassLogger() ? I noticed container.RegisterConditional() has access to the context. Also, I want to avoid solutions like SimpleLogging.NLog for now. Finally, I am willing to accept this is not the right approach. BTW, AOP is an option that I've already explored (Is it a good practice to have logger as a singleton?). Note: I am aware that

Simple Injector FilterInjection seems to be reinitialising RegisterPerWebRequest injected item

元气小坏坏 提交于 2019-12-09 06:39:25
I'm trying to move from Ninject to Simple Injector but I'm experiencing an odd issue when trying to duplicate functionality that worked with Ninject. In Ninject I had a service which contained: private readonly ICollection<Message> messages; This service was registered as Bind<INotificationService>().To<NotificationService>() .InRequestScope(); This service allowed messages (UI and error) to be passed back to the MVC site. This service was injected into an ActionFilterAttribute: kernel.BindFilter<CriticalErrorAttribute>(FilterScope.Last, 1) .When((context, ad) => !string.IsNullOrEmpty(ad

Does Simple Injector have a way to RegisterConditional with object factory?

℡╲_俬逩灬. 提交于 2019-12-09 05:37:28
问题 I see Simple Injector's Container has this method public void RegisterConditional<TService, TImplementation>( Predicate<PredicateContext> predicate ) But I want to use the different object of the same implementation for different service, so what overloaded method I need would look like this public void RegisterConditional<TService>( Func<TService> instanceCreator, Predicate<PredicateContext> predicate ) But the SimpleInjector doesn't have it. I am trying to find the other Container's methods

Register generic types with constructor in SimpleInjector how?

情到浓时终转凉″ 提交于 2019-12-08 09:23:49
问题 I use Microsoft Unity as IoC container and wrote some code like this: public static void RegisterTypes(IUnityContainer container) { MyContext ctx = new MyContext (); // is EntityFramework DbContext container.RegisterType(typeof(IEntityRepository<>), typeof(EntityRepository<>), new InjectionConstructor(ctx)); container.RegisterType(typeof(IEntityService<>), typeof(EntityService<>)); } It works for me without problem, but I want to know how to write its equivalent in SimpleInjector . 回答1: There

Simple Injector inject dependency into custom global authentication filters and OWIN middle ware OAuthAuthorizationServerProvider

被刻印的时光 ゝ 提交于 2019-12-08 09:01:26
问题 I used Simple Injector as our Ioc container; we have two problems. We want to inject into our custom authentication filter; we read the post of converting attribute to a passive attribute: Convert Attribute into a passive. But we can't convert custom authentication filter attribute into a passive. public class BearerAuthentication : Attribute, IAuthenticationFilter { public async Task AuthenticateAsync( HttpAuthenticationContext context, CancellationToken cancellationToken) { } public Task