simple-injector

Simple Injector property injection on action filter

风格不统一 提交于 2019-11-30 07:15:03
问题 The action filter I want to inject into starts like this public class UserAuthorisation : AuthorizeAttribute { public IWcfClientProxy<IAppFrameworkServiceChannel> FrameworkServiceProxy { get; set; } I have setup my container like this: container.Register<IWcfClientProxy<IAppFrameworkServiceChannel>>( ()=> new WcfClientProxy<IAppFrameworkServiceChannel>()); container.RegisterInitializer<UserAuthorisation>(handler => { handler.FrameworkServiceProxy = container .GetInstance<IWcfClientProxy

Using Application Insights with Unit Tests?

≡放荡痞女 提交于 2019-11-30 06:45:37
问题 I have an MVC web app, and I'm using Simple Injector for DI. Almost all my code is covered by unit tests. However, now that I've added some telemetry calls in some controllers, I'm having trouble setting up the dependencies. The telemetry calls are for sending metrics to the Microsoft Azure-hosted Application Insights service. The app is not running in Azure, just a server with ISS. The AI portal tells you all kinds of things about your application, including any custom events you send using

How do I pass a parameter to the constructor using Simple Injector?

风流意气都作罢 提交于 2019-11-30 01:08:44
问题 Does Simple Injector allow you to pass parameters to constructor when you resolve? I'd like to know if both these frameworks do what Unity's ResolverOverride or DependencyOverride both do. 回答1: I suspect that this question is about passing primitive values to the constructor at the time the service is actually resolved. Let's set up a simple test class: public interface IFoo { } public class Foo : IFoo { public Foo(string value) { } } The Foo class takes a string argument that we would like

Decorators and IDisposable

ε祈祈猫儿з 提交于 2019-11-30 00:51:59
问题 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

Simple Injector, can't override existing registration

我只是一个虾纸丫 提交于 2019-11-29 21:12:43
问题 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

ICommandHandler/IQueryHandler with async/await

筅森魡賤 提交于 2019-11-29 21:06:18
EDITH says (tl;dr) I went with a variant of the suggested solution; keeping all ICommandHandler s and IQueryHandler s potentially aynchronous and returning a resolved task in synchronous cases. Still, I don't want to use Task.FromResult(...) all over the place so I defined an extension method for convenience: public static class TaskExtensions { public static Task<TResult> AsTaskResult<TResult>(this TResult result) { // Or TaskEx.FromResult if you're targeting .NET4.0 // with the Microsoft.BCL.Async package return Task.FromResult(result); } } // Usage in code ... using TaskExtensions; class

Simple Injector per-web-api-request dependency in SignalR hub

蹲街弑〆低调 提交于 2019-11-29 20:00:17
问题 According to this post, it should be possible to inject per-web-request dependencies into SignalR hubs (although with some limitations like problem with OnDisconnected() method). In my case it is ASP Web API (not MVC) and it does not work for some reason. Here are relevant parts: container.RegisterWebApiControllers(httpConfiguration); container.RegisterWebApiRequest<DbContext, MyDbContext>(); container.RegisterWebApiRequest<ISampleRepository, SampleRepository>(); //DbContext injected to

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

允我心安 提交于 2019-11-29 18:24:15
问题 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

How to use WPF controls with Simple Injector dependencies

风流意气都作罢 提交于 2019-11-29 15:11:13
I'd like to use Dependency Injection in a scenario where I have to inject resources into GUI-controls. As that might be the wrong Place, I have some reasons to do it here and not in a view model (eg. I need Window handles and such). Constructor parameter injection seems to be the preferred way. As most of you know WPF controls must have a parameter-less constructors, otherwise the XAML does not work and for the current scenario I love to keep my XAML since it contains some name registrations and bindings. So: How can I use constructor-DI in a WPF+XAML scenario and (if possible in the case of

How to, using dependency injection, get configuration from multiple sources?

会有一股神秘感。 提交于 2019-11-29 14:09:24
问题 I'm using Simple Injector, but maybe what I need is more of a conceptual answer. Here's the deal, suppose I have an interface with my application settings: public interface IApplicationSettings { bool EnableLogging { get; } bool CopyLocal { get; } string ServerName { get; } } Then, one would usually have a class which implements IApplicationSettings, getting each field from a specified source, for instance: public class AppConfigSettings : IApplicationSettings { private bool? enableLogging;