SimpleInjector | fast Dependency Injection | Dependency Injection library
kandi X-RAY | SimpleInjector Summary
kandi X-RAY | SimpleInjector Summary
An easy, flexible, and fast Dependency Injection library that promotes best practice to steer developers towards the pit of success.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of SimpleInjector
SimpleInjector Key Features
SimpleInjector Examples and Code Snippets
Community Discussions
Trending Discussions on SimpleInjector
QUESTION
i'm trying to follow this example here
the only thing is i need to inject multiple hosted services instead of one like this
...ANSWER
Answered 2022-Mar-05 at 09:29I'm unable to reproduce your issue. I used the following code in a Visual Studio 2022 ASP.NET Core MVC 6 project:
QUESTION
I want to reuse same class more than once in a chain of decorated objects like this.
InstrumentedTargetDecorator -> ResilientTargetDecorator -> InstrumetedTargetDecorator -> Target
Note that InstrumentedTargetDecorator
is used twice here on purpose.
This is how SimpleInjector
syntax looks to get the above wired up
ANSWER
Answered 2022-Jan-29 at 16:13You encountered a bug in Simple Injector. I will try to fix this in the next patch release. This bug has been fixed in v5.3.3.
As workaround, when working in an older release, create a derivative of the decorator inside your Composition Root and make that derivative the second registration. Hopefully this suppresses the invalid warning:
QUESTION
I have an object whose constructor requires a primitive. I register it using a delegate (please excuse the contrived example).
...ANSWER
Answered 2021-Dec-23 at 09:46Try this:
QUESTION
The issue is as follows:
I have to access 2 different S3 buckets and the way I have to create AmazonS3clients is as follows:
...ANSWER
Answered 2021-Nov-22 at 13:05Here is the way to do it thanks to @Steven:
QUESTION
We currently use SimpleInjector as our DI container to implement the command-query-separation (CQS) pattern by using ICommand
and IQuery
and ICommandHandler
and IQueryHandler
interfaces.
We also use the decorator pattern to perform aspect-oriented programming. For one of our decorators we use FluentValidation to perform validation logic for specific commands or queries.
With SimpleInjector, it is possible to conditionally register types. This is useful for our FluentValidation decorator when we have a command or query which does not have a corresponding validator. Then we use a NullValidator
as fallback which does nothing. Exactly this scenario is described in the SimpleInjector documentation and looks like this for the FluentValidation scenario:
ANSWER
Answered 2021-Nov-02 at 08:14I managed to work around this issue by injecting an IEnumerable>
in the FluentValidationCommandHandlerDecorator
decorator instead of one single IValidator
instance. In that case I don't need a NullValidator
at all and an empty collection gets injected if no validator is available for the specific command or query.
SimpleInjector must be changed to register a collection of IValidator
instances:
QUESTION
I would like to get some clarification in regards to how loose the execution timing is for static field initializers in .NET. I'm getting a TypeInitializationException when running the code below in an online compiler(Fiddle), but I get the expected output of 1 in a test console app in VS.
Code:
...ANSWER
Answered 2021-Jul-25 at 08:56According to the C# language spec, this is specifically pointed out as an implementation-dependent thing:
If a static constructor exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class.
The only guarantee you get, is that it must be some time before the first use of a static field.
However, as you probably have found out in your fiddle, adding a static constructor makes this work. A static constructor guarantees that static field initialisers to be run immediately before the static constructor. The good news is that the execution of the static constructor is not implementation-dependent (spec):
The execution of a static constructor is triggered by the first of the following events to occur within an application domain:
- An instance of the class type is created.
- Any of the static members of the class type are referenced.
QUESTION
We have a couple of functions in a function app based on a consumption plan. When I stress test these functions with JMeter (200 threads simultaneously), the functions are throwing SimpleInjector.ActivationException's for about 50% of the time. Not all requests are failing.
I don't get why this is only for a part of the requests.
Call stack:
2021-07-06T08:43:41.159 [Error] Er is een probleem opgetreden
SimpleInjector.ActivationException : The constructor of type GetLocalKeyDataByDateQueryHandler contains the parameter with name 'queryValidator' and type IValidator, but IValidator is not registered. For IValidator to be resolved, it must be registered in the container.at SimpleInjector.Container.ThrowParameterTypeMustBeRegistered(InjectionTargetInfo target)at SimpleInjector.Advanced.DefaultDependencyInjectionBehavior.GetInstanceProducer(InjectionConsumerInfo dependency,Boolean throwOnFailure)at SimpleInjector.ContainerOptions.GetInstanceProducerFor(InjectionConsumerInfo consumer)at SimpleInjector.Registration.BuildConstructorParameters(ConstructorInfo constructor)at SimpleInjector.Registration.BuildNewExpression()at SimpleInjector.Registration.BuildTransientExpression()at SimpleInjector.Registration.BuildTransientDelegate()at SimpleInjector.Lifestyles.ScopedRegistration.BuildExpression()at SimpleInjector.InstanceProducer.BuildExpressionInternal()at SimpleInjector.Internals.LazyEx`1.InitializeAndReturn()at SimpleInjector.InstanceProducer.BuildInstanceCreator()at SimpleInjector.InstanceProducer.BuildAndReplaceInstanceCreatorAndCreateFirstInstance()at SimpleInjector.InstanceProducer.GetInstance()at SimpleInjector.Container.GetInstanceTServiceat async ProRail.PUIC2.Web.Functions.GetPuicDataByChangeDate.Run(GetPuicDataByDate inputParams,ILogger logger,ExecutionContext context) at D:\a\1\s\Src\Web\ProRail.PUIC2.Web.Functions\Functions\GetPuicDataByChangeDate.cs : 35
Function:
...ANSWER
Answered 2021-Jul-12 at 14:47It seems there is a multi-threading issue with your code; the DependencyInjection.GetContainerInstance
method.
GetContainerInstance
possibly creates many Container
instances when called in parallel and the configuration of the final Container
instance will be undetermined.
To fix this issue, you will have to synchronize the creation of the container. There are many ways to do this, but you can, for instance, try this:
QUESTION
Per https://github.com/App-vNext/Polly/wiki/Polly-and-HttpClientFactory#extending-the-convenience-addtransienthttperrorpolicy-definition I can see policies are added with a name.
...ANSWER
Answered 2021-Jun-24 at 08:45I'm not sure I can give you a satisfying answer, but here's an idea for a possible implementation. You can make the IPollyHttpClientFactory
conditional, in a way that each consumer gets its own version. This can be done using the RegisterConditional
method. For instance:
QUESTION
I have values in my AppConfig like this...
...ANSWER
Answered 2021-Jun-08 at 21:39You register type AppConfig
as singleton implementation of type IAppConfig
, so when you request IAppConfig
from container, you'll get an instance created by container, and not 'appConfig' you created in Start
.
You need to register instance like:
QUESTION
I am trying to use Simple Injector as the DI container for Caliburn.Micro. Demo source: https://github.com/nvstrien/WPFDemos
This project has a basic Caliburn.Micro setup with a Simple Injector Container. The ShellView has 1 button and when pressed, an async method is called to get some simulated data.
I am getting this error in Bootstrapper.Buildup.
SimpleInjector.ActivationException: 'No registration for type SequentialResult could be found. Make sure SequentialResult is registered, for instance by calling 'Container.Register();' during the registration phase. An implicit registration could not be made because Container.Options.ResolveUnregisteredConcreteTypes is set to 'false', which is now the default setting in v5. This disallows the container to construct this unregistered concrete type. For more information on why resolving unregistered concrete types is now disallowed by default, and what possible fixes you can apply, see https://simpleinjector.org/ructd. '
It has been suggested here that commenting out Bootstrapper.BuildUp should work: Caliburn.Micro Bootstrapper 'BuildUp' method throws exception when Simple Injector is used
However, when doing so, SimpleInjector will still throw an exception.
Any help solving this would be greatly appreciated
My complete Bootstrapper config file looks like this:
...ANSWER
Answered 2021-May-14 at 21:51@Steven: Commenting out BuildUp indeed fixed my problem.
I thought that I had tested commenting out BuildUp in my sample project before coming to SO to ask my question, but trying it again now solved my problem. Thank you for putting me back on the right track!
Solution: comment out / delete BootStrapper.BuildUp as was also suggested here: Caliburn.Micro Bootstrapper 'BuildUp' method throws exception when Simple Injector is used
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install SimpleInjector
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page