autofac

Dynamic menu creation IoC

六月ゝ 毕业季﹏ 提交于 2019-12-02 11:10:01
I am wondering if anyone out there knows how I could create how could i use something like AutoFac to let me dynamically allow dll's to create there own forms and menu items to call them at run time. So if I have an, Employee.dll New Starter Form Certificate Form Supplier.dll Supplier Detail From Products Form In my winform app it would create a menu with this and when each one clicked load the relavent form up People New Starter Certificate Supplier Supplier Details Products So I can add a new class library to the project and it would just add it to menu when it loads up. Hope that make sense

Why does the DatabaseInitializer get called twice?

穿精又带淫゛_ 提交于 2019-12-02 09:29:50
I’ve inherited an MVC that currently does some setup work with the ApplicationStart method so that when the application comes back to life with an IIS Application pool this setup has already been carried out. As pseudo-code: public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { // Build Api autofac container // Build MVC autofac container // Resolve serviceOne from the MVC container var serviceOne = (IServiceOne)DependencyResolver.Current.GetService(typeof(IServiceOne)); // Make setup call - includes external http calls and DbContext checking

Autofac: Resolving dependencies with parameters

我们两清 提交于 2019-12-02 06:32:55
问题 I'm currently learning the API for Autofac, and I'm trying to get my head around what seems to me like a very common use case. I have a class (for this simple example 'MasterOfPuppets') that has a dependency it receives via constructor injection ('NamedPuppet'), this dependency needs a value to be built with (string name): public class MasterOfPuppets : IMasterOfPuppets { IPuppet _puppet; public MasterOfPuppets(IPuppet puppet) { _puppet = puppet; } } public class NamedPuppet : IPuppet {

Autofac实现AOP拦截

混江龙づ霸主 提交于 2019-12-02 05:36:49
本文主要是详解一下在ASP.NET Core中,采用替换后的Autofac来实现AOP拦截。 Aspect Oriented Programming(AOP),面向切面编程,是一个比较热门的话题。AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果。 引入类库 nuget命令如下: Install-Package Autofac.Extras.DynamicProxy -Version 4.5.0 复制代码 <ignore_js_op> 采用Autofac来实现AOP 首先,我们创建一个拦截类,代码如下: public class AOPTest : IInterceptor { public ILogger<AOPTest> _logger { get; set; } public void Intercept(IInvocation invocation) { _logger.LogWarning("你正在调用方法 "{0}" 参数是 {1}... ", invocation.Method.Name, string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray())); /

Autofac with Web API 2 - Parameter-less constructor error

折月煮酒 提交于 2019-12-02 03:43:17
问题 I am trying to get Autofac setup on my Web API 2 project. I have only used it a couple of times before. I used Nuget an installed both Autofac and Autofac.WebApi2 . Then in my Startup class I did this: public partial class Startup { public void Configuration(IAppBuilder app) { // Get our http configuration var config = new HttpConfiguration(); // Register the Autofac middleware FIRST. This also adds // Autofac-injected middleware registered with the container. var container =

Autofac with Web API 2 - Parameter-less constructor error

走远了吗. 提交于 2019-12-02 00:37:44
I am trying to get Autofac setup on my Web API 2 project. I have only used it a couple of times before. I used Nuget an installed both Autofac and Autofac.WebApi2 . Then in my Startup class I did this: public partial class Startup { public void Configuration(IAppBuilder app) { // Get our http configuration var config = new HttpConfiguration(); // Register the Autofac middleware FIRST. This also adds // Autofac-injected middleware registered with the container. var container = ConfigureInversionOfControl(app, config); // Register all areas AreaRegistration.RegisterAllAreas();

Autofac Generic Service resolution at runtime

天涯浪子 提交于 2019-12-02 00:05:00
Recently, Castle added support for interface factories with implementations provided by the kernel. I am wondering if there is a way to do this in autofac also. I have read about the delegate factories, but I think I might be missing something, and am unable to get it to work. Here is what I am thinking: class Message { } interface IHandle<T> { void Handle(T message); } class Handle<Message> : IHandle<Message> { ... } class Bus { .ctor (? lookup) { _lookup = lookup; } void Send<T>(T message) { _lookup.GetHandler<T>().Handle(message); } } var builder = new ContainerBuilder(); builder

ASP.NET Core 依赖注入

陌路散爱 提交于 2019-12-01 23:52:43
原文地址: https://www.cnblogs.com/youring2/p/10926590.html ASP.NET Core从框架层对依赖注入提供支持。也就是说,如果你不了解依赖注入,将很难适应 ASP.NET Core的开发模式。本文将介绍依赖注入的基本概念,并结合代码演示如何在 ASP.NET Core中使用依赖注入。 什么是依赖注入? # 百度百科对于依赖注入的介绍: 控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找”(Dependency Lookup)。通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中。 从百科的介绍中可以看出,依赖注入和控制反转是一回事,依赖注入是一种新的设计模式,通过正确使用依赖注入的相关技术,可以降低系统耦合度,增加系统的可扩展性。 我们来看一个例子: Copy public interface IInterfaceA { } public interface IInterfaceB { } public class ClassA : IInterfaceA {

JsonConverter Attribute : deserialize using custom constructor and Autofac

孤街浪徒 提交于 2019-12-01 23:31:43
Am using a custom JsonConverter to convert my JSON object. This is achieved via the JsonConverter attribute to the IQuery object below [JsonConverter(typeof(CustomConverter<IQuery>))] public interface IQuery { } The custom generic class is below (some bits removed for brevity) public class CustomConverter<T> : JsonConverter { // This should be created via AutoFac public ICustomObjectCreator<T> ObjectCreator { get; set; } // This default constructr always gets called public CustomConverter() {} // I want to call this constructor [JsonConstructor] public CustomConverter(ICustomObjectCreator<T>

How to configure Autofac and SignalR in a MVC 5 application

蹲街弑〆低调 提交于 2019-12-01 21:28:36
I am trying to configure an MVC 5 application to use SignalR 2.2.x and inject a service into my NotificationsHub . We are using Autofac for MVC but I am not sure on how to correctly configure this. Autofac documentation exists for NuGet Autofac.Integration.SignalR (3.0.2) and Autofac.Integration.Mvc (3.3.4) . What I am doing so far is register the hubs via: ContainerBuilder builder = new ContainerBuilder(); // Register MVC controllers. builder.RegisterControllers(typeof(MvcApplication).Assembly); builder.RegisterType<ServiceForSignalRHub>().AsImplementedInterfaces(); builder.RegisterType<...>(