simple-injector

How to get OwinContext from Global.asax?

女生的网名这么多〃 提交于 2019-12-03 11:14:47
I am trying to set up my Dependency Injection and I am in the need of injecting a IAuthenticationManager from ASP.NET Identity to an OwinContext . For this I am from my Global.asax -> ServiceConfig.Configure() running: container.Register(() => HttpContext.Current.GetOwinContext().Authentication); But when I am running my application I get this message: No owin.Environment item was found in the context Why is this HttpContext.Current.GetOwinContext() not available from Global.asax? Startup.cs [assembly: OwinStartupAttribute(typeof(MyApp.Web.Startup))] namespace Speedop.Web { public partial

Registering NLog ILogger with Simple Injector

╄→гoц情女王★ 提交于 2019-12-03 11:07:29
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 GetCurrentClassLogger() gets the same information I'd retrieve with .NET reflection. using NLog; using

Simple Injector fails to inject per Web API request registered class during Owin startup

你离开我真会死。 提交于 2019-12-03 10:59:30
I'm creating an API using Owin, Web API, Entity Framework, ASP.NET Identity. I'm using Simple Injector as my DI framework of choice. During the Owin startup process, I want to seed my database with some sample data. This is handled by a class implementing IDatabaseInitializer , which looks something like this: public class MyDbInitializer : DropCreateDatabaseAlways<MyDataContext> { private readonly IUserManager _userManager; public MyDbInitializer(IUserManager userManager) { _userManager = userManager; } protected override void Seed(MyDataContext context) { SeedIdentities(); } private void

Using RegisterInitializer to wire event handlers

狂风中的少年 提交于 2019-12-03 09:13:18
I have a WCF service that uses Simple Injector for dependency injection. I want to wire up some event handlers in the container bootstrapper. I have created an interface IStatusChangeNotification : public interface IStatusChangeNotification { event EventHandler<int> JobStatusChange; } My CommandHandler implements IStatusChangeNotification and there are two event handler classes EmailNotification and MmrNotification , each defining a Notify() method. Then in my bootstrap code I have the following: container.Register<EmailNotification>(); container.Register<MmrNotification>(); container

Unable to resolve a controller that was loaded from external dll

半城伤御伤魂 提交于 2019-12-03 09:02:23
I am building a Web API using MVC4 Web API with an IoC container (Simple Injector in this case, but I don't think this problem is related to that container) that should expose a variety of CRUD and query operations. The reason for using IOC in my case is that we are a dev shop and I need to be able to let customers build their own web API controllers to expose the data they need to expose need from our system. Consequently, I was hoping to design my solution in a way that allowed me to dogfood my own product by making all the controllers, both ours and our customers', external and loadable

Configure decorators for generic interfaces and inject all instances to constructor with non generic interface argument in Simple Injector

五迷三道 提交于 2019-12-03 08:56:15
I've been using a pattern very similar to what is described in this excellent article to have commands and queries as objects. I am also using SimpleInjector as the DI container. The only significant difference is that rather that controller take an explicit dependency on some ICommandHandler<TCommand> I want the controllers to take a dependency on an object (a Dispatcher ) which will take a ICommand instance and resolve the correct handler for that command. This will reduce the number of parameters that the constructors need to take and make the whole thing a little easier to use. So my

What is the correct way to register FluentValidation with SimpleInjector?

守給你的承諾、 提交于 2019-12-03 07:12:46
I am able to register FluentValidation AbstractValidators using a FluentValidatorFactory . However, it doesn't feel right, because not all of the IoC container registrations happen during bootstrap / composition root. Instead, the fluent validators are registered by a separate factory : The composition root : public class SimpleDependencyInjector : IServiceProvider { public readonly Container Container; public SimpleDependencyInjector() { Container = Bootstrap(); } internal Container Bootstrap() { var container = new Container(); container.Register< // ...register all non-fluent-validator

How to configure simple injector container and lifestylse in a MVC web app with WebAPI, WCF, SignalR and Background Task

て烟熏妆下的殇ゞ 提交于 2019-12-03 05:56:26
The simple injector documentation provides great examples on how to setup the container for WebRequest, Web API, WCF, ... but the examples are specific to one technology/lifestyle at a time. Our web application uses most of them together! It is not clear to me how to configure the container to work with several lifestyles. Let's say I have a MVC project with Web API. I have the following objects: MyDbContext : My entity code first db context IMyDataProvider implemented by MyDataProvider : Contains query logic and uses MyDbContext MyController : MVC controller that uses IMyDataProvider

Simple Injector: Inject same UnitOfWork instance across services of the same graph

大憨熊 提交于 2019-12-03 05:19:16
问题 I have multiple services, each of which have a UnitOfWork injected into the constructor using the Simple Injector IoC container. Currently I can see each UnitOfWork instance is a separate object, this is bad as i am using Entity Framework and require the same context reference across all units of work. How can I ensure the same UnitOfWork instance is injected into all services per each resolve request? My UnitOfWor will be saved by an external command handler decorator after the command

Implementing Domain Event Handler pattern in C# with Simple Injector

烂漫一生 提交于 2019-12-02 19:51:00
I am trying to implement the Domain Event pattern in C# using Simple Injector . I have simplified my code to be in one file that can be ran as a console app and have excluded the Simple Injector code to keep things clear for the purpose of this question. The problem I am coming up against is that each event could have multiple event handlers and multiple events could be raised but I want to restrict my Dispatcher to only handle events that implement the IEvent interface so I put that restraint on my Dispatch method. This caused problems as to how to get the instance from Simple Injector as