simple-injector

Wiring up Simple Injector in WebForms in .NET 4.7.2

天涯浪子 提交于 2019-12-01 05:40:36
With the changes in .NET 4.7.2, constructor injection is now possible in Web Forms. I have gotten Simple Injector working with Web Forms, but would like some input as to if there any "gotchas" I might be missing. First I have the registration of the Pages themselves which is taken from here . public static void RegisterWebPages(this Container container) { var pageTypes = from assembly in BuildManager.GetReferencedAssemblies().Cast<Assembly>() where !assembly.IsDynamic where !assembly.GlobalAssemblyCache from type in assembly.GetExportedTypes() where type.IsSubclassOf(typeof(Page)) where !type

How to decorate an ASP.NET MVC controller with Simple Injector

拈花ヽ惹草 提交于 2019-12-01 00:30:24
问题 I'd like to apply some cross-cutting concerns to my MVC controllers. At the moment, this is implemented through an abstract base class, but as we are refactoring more of the code base to take advantage of dependency injection, I'm wondering if this is something Simple Injector can help me with through its decoration or interception facilities. So I've attempted to create a pretty basic decorator: public class ControllerDecorator : IController { private readonly IController _controller; public

Simple Injector - Register foreach type with parameter

末鹿安然 提交于 2019-11-30 20:41:33
问题 I've been using Autofac as DI Container and it's great, but it's a bit too slow. So I've decided to migrate my project to Simple Injector, because I've seen some benchmark tests and it's one of the fastest. Its API however lack a bit in my opinion, but I'm sure there are some workaround to solve my problem. I'm scanning two assemblies to get their types because I want to register them in an automated way: var repositories = (from type in typeof(DataRepository).Assembly.GetExportedTypes()

Decorators and IDisposable

不羁岁月 提交于 2019-11-30 17:30:47
I have a subclass of DbContext public class MyContext : DbContext { } and I have an IUnitOfWork abstraction around MyContext that implements IDisposable to ensure that references such as MyContext are disposed of at the appropriate time public interface IUnitOfWork : IDisposable { } public class UnitOfWork : IUnitOfWork { private readonly MyContext _context; public UnitOfWork() { _context = new MyContext(); } ~UnitOfWork() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private bool _disposed; protected virtual void Dispose(bool disposing) { if (

Simple Injector, can't override existing registration

左心房为你撑大大i 提交于 2019-11-30 14:18:42
I am currently using Simple Injector for the first time. In my .NET project I am running test and mocking data returned from a web service and registering the object to the container like so _container.Register<IWebServiceOrder>(() => mock.Object, Lifestyle.Transient); This works fine. But in my tests I want to test the behavior of the system on a second call to the web service that will contain updated data, so the mock object will need to be updated. By default Simple Injector does not allow Overriding existing registrations but the official website states it is possible to change this

Injecting Simple Injector components into IHostedService with ASP.NET Core 2.0

只愿长相守 提交于 2019-11-30 12:39:57
In ASP.NET Core 2.0, there is a way to add background tasks by implementing the IHostedService interface (see https://docs.microsoft.com/en-us/aspnet/core/fundamentals/hosted-services?view=aspnetcore-2.0 ). By following this tutorial, the way I was able to get it working was by registering it in the ASP.NET Core container. My goal is to be reading messages from a queue and processing the jobs in the background; a message is posted to the queue (via controller action) and then processed in the background on a timed interval. // Not registered in SimpleInjector services.AddSingleton

Simple Injector: Register ILogger<T> by using ILoggerFactory.CreateLogger<T>()

泄露秘密 提交于 2019-11-30 12:11:55
问题 I'm working with a project which utilizes Simple Injector as dependency injector. On the other hand, this project uses Microsoft.Extensions.Logging in order to log the events that occurs in certain classes. My technical issue is pretty simple to explain. I want to register in my DI the ILogger independently of the class T which is being invoked, but I DO NEED to do it from my ILoggerFactory.CreateLogger<T>() method because this gets the logger configuration using Microsoft.Extensions

How to use Simple injector, Repository and Context - code first

假装没事ソ 提交于 2019-11-30 08:54:59
I'm trying to use Simple Injector to create my repository and use it in the Business logic layer ( also i want to use PerWebRequest method ) . In the DAL layer i have : public interface IRepository<T> where T : class { void Add(T entity); void Delete(T entity); void Delete(int id); void Update(T entity); T GetById(int Id); IQueryable<T> All(); IEnumerable<T> Find(Func<T, bool> predicate); } and : public class EFRepository<T> : IRepository<T>, IDisposable where T : class { #region Members protected DbContext Context { get; set; } protected DbSet<T> DbSet { get; set; } #endregion #region

WebApi + Simple Injector + OWIN

ε祈祈猫儿з 提交于 2019-11-30 08:02:10
问题 I am trying to use SimpleInjector with OWIN in a WebAPI project. However, the following line in ConfigureAuth fails app.CreatePerOwinContext(container.GetInstance<ApplicationUserManager>); The exception is The ApplicationUserManager is registered as 'Web API Request' lifestyle, but the instance is requested outside the context of a Web API Request. I am using container.RegisterWebApiRequest<ApplicationUserManager>(); in container initialization. (there won't be any exceptions if I use

Does Simple Injector supports MVC 4 ASP.NET Web API?

被刻印的时光 ゝ 提交于 2019-11-30 07:24:40
问题 I am new to Simple Injector IOC container. I will start working in a project which will require a Multi-tenant ASP.NET MVC implementation using MVC 4 ASP.NET Web API. My question is: Does Simple injector support MVC 4 ASP.NET Web API? Reading simple injector documentation like this makes references to MVC 3 and I would like to know if MVC 4 also is supported. 回答1: Does Simple injector IOC support MVC 4 ASP.NET Web API? It has currently no support for MVC4 Web API, but support will be added in