Tag: ioc container

什么是IoC容器中的自我绑定?

我已经看到像Ninject这样的框架以及Stack上的post在使用dependency injection框架时会谈到自我绑定,如下面的代码所示。 Bind().To(); 他们甚至达到了具有特殊语法的程度: Bind().ToSelf(); 为什么要将类型绑定到自身? 我没有看到任何实际的应用程序,它可能有用,并有助于减少代码中的依赖性。 这不仅仅意味着对类型的引用会简单地解决它自己吗?

如何从.net核心IoC容器中删除默认服务?

关于.net核心的一个美妙的事情是它非常模块化和可配置。 这种灵活性的一个关键方面是它通过接口利用IoC来注册服务。 理论上,这允许用很少的努力用该服务的自定义实现替换默认的.net服务。 理论上这一切听起来都很棒。 但我有一个真实的工作案例,我想用我自己的替换默认的.net核心服务,我无法弄清楚如何删除默认服务。 更具体地说,在Startup.cs ConfigureServices方法中,当调用services.AddSession() ,它会在代码后注册DistributedSessionStore vai: services.AddTransient(); 可以在源代码中看到: https : //github.com/aspnet/Session/blob/rel/1.1.0/src/Microsoft.AspNetCore.Session/SessionServiceCollectionExtensions.cs 我想用我自己创建的那个替换那个ISessionStore。 所以,如果我有一个类RonsSessionStore:ISessionStore我想用来替换当前注册的ISessionStore的ISessionStore,我该怎么办呢? 我知道我可以通过以下方法在Startup.cs ConfigureServices方法中注册我的ISessionStore: services.AddTransient(); 但是,如何删除已注册的DistributedSessionStore ? 我尝试在startup.cs ConfigureServices方法中完成此ConfigureServices services.Remove(ServiceDescriptor.Transient()); 但它没有任何效果,DistributedSessionStore仍然在IoC容器中。 有任何想法吗? 如何在startup.cs的ConfigureServices方法中从IoC中删除服务?

“注入”属性不适用于字段

Inject属性不适用于字段。 [Inject] public MyContext context; //Not injected [Inject] public MyContext context {get; set;} //Injected 我正在使用默认的Ninject设置。 为什么不注入田地?

在DI原则下应该注入什么样的C’tor参数?

我试图了解应该将哪些对象注入对象以及哪些对象应该在内部创建。 如果我有一些List (作为数据字段),它包含在运行时收集的信息。 似乎我应该在c’tor中启动它而不是注入它。 但是通过COM端口进行通信的硬件类呢? 我让HW类初始化SerialPort还是我注入它? 如果需要注入上述SerialPort; 最好的方法是什么? 我手动创建它: SerialPort port = new SerialPort(name, baud …); HWClass hwClass = container.Reolve(“HWClass”, new InjectionConstructor(port)); 或使用Unity容器 SerialPort port = conatiner.Resolve(…); HWClass hwClass = container.Reolve(“HWClass”, new InjectionConstructor(port)); 或者我应该在HWClass C’tor内部启动它? adiel

IoC容器并发症的C#ASP.NETdependency injection

我为这个长度道歉,我知道这里有一些答案,但我搜索了很多,但没有找到正确的解决方案,所以请耐心等待。 我正在尝试为遗留应用程序创建一个框架,以便在ASP.NET webforms中使用DI。 我可能会使用Castle Windsor作为框架。 这些遗留应用程序将在某些地方部分使用MVP模式。 演示者看起来像这样: class Presenter1 { public Presenter1(IView1 view, IRepository userRepository) { } } 现在ASP.NET页面看起来像这样: public partial class MyPage1 : System.Web.UI.Page, IView1 { private Presenter1 _presenter; } 在使用DI之前,我会在页面的OnInit中按如下方式实例化Presenter: protected override void OnInit(EventArgs e) { base.OnInit(e); _presenter = new Presenter1(this, new UserRepository(new SqlDataContext())); } 所以现在我想使用DI。 首先,我必须创建一个处理程序工厂来覆盖我的页面的构造。 我发现这是一个非常好的答案: 如何在ASP.NET Web窗体中使用dependency injection 现在我可以轻松地在我的组合根中设置我的容器,因为Mark Seeman建议使用Global.asax(这意味着创建一个必须是线程安全且密封的静态容器,不能再添加注册) […]

为什么Castle Windsor试图将我的“内容”和“脚本”文件夹解析为控制器?

我创建了一个ASP.NET MVC应用程序,并尝试使用Castle Windsor作为我的IOC 但是,当控制器试图解决时,我将’Content’和’Scripts’放入CreateController(RequestContext requestContext, string controllerName)方法中的’controllerName’参数中。 不用说这些不是控制器。 它们似乎是网站的文件夹 为什么要尝试将这些注册为控制器? 我该如何忽略这些文件夹? 谢谢 来自WindsorControllerFactory的exception 由于无法发布图像我必须描述它 – 它基本上只是说 ‘找不到内容控制器’ 的Global.asax.cs public static IIocContainer Ioc; protected void Application_Start() { InitialiseIocContainer(); RegisterViewEngine(ViewEngines.Engines); RegisterRoutes(RouteTable.Routes); StartProfiling(); } private void InitialiseIocContainer() { IWindsorContainer _container = new WindsorContainer(); var controllerTypes = typeof (GidgetController).Assembly.GetTypes(); foreach (var controllerType in controllerTypes.Where((t=>typeof(IController).IsAssignableFrom(t)))) { _container.AddComponentLifeStyle(controllerType.Name.ToLower(), controllerType, LifestyleType.Transient); } […]

AutoFac – 为一些开放的Generic注册装饰器

我正在尝试设置一个似乎有复杂要求的Autofac模块。 开始: 我有一个通用的界面: public interface IMyInterface 我有一堆实现此接口的类 例如 class MyImpl1 : IMyInterface { } class MyImpl2 : IMyInterface { } class MyImpl3 : IMyInterface { } 最后,我有一个装饰者: class MyDecorator : IMyInterface 我只想“装饰”具有特定属性的( MyInterface )实现。 所以具有[MyAttribute]属性的MyInterface的所有实现都使用MyDecorator进行修饰。 我很近但没有雪茄: var builder = new ContainerBuilder(); builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) .Where(type => type.GetCustomAttributes(true) .Any(attr => attr.GetType() == typeof(MyAttribute))) .AsClosedTypesOf(typeof (IMyInterface)) .Keyed(“CachableQueries”, typeof(IMyInterface)); builder.RegisterGenericDecorator(typeof(MyDecorator), […]

Autofac将参数传递给嵌套类型

我在我的WCF服务中使用Autofac作为我的IoC。 我有一种情况,我想将一个对象传递给嵌套类型(即一种未直接解析的类型,但在解析另一种类型时)。 据我所知,将此对象作为构造函数参数传递是Autofac中的首选方法。 以下是这种情况的一个例子。 嵌套类型: public class EventLogger : IEventLogger { public EventLogger(IRepository repository, User currentUser) { … } } 我实际上试图解决的类型: public class SomeBusinessObject { public SomeBusinessObject(IEventLogger logger, …) { … } } 注册: var builder = new ContainerBuilder(); builder.RegisterGeneric(typeof(Repository)).As(typeof(IRepository)); builder.RegisterGeneric(typeof(EventLogger)).As(typeof(IEventLogger)); builder.RegisterType(); 解析我的WCF服务操作: var currentUser = GetUserFromServiceContext(); var bo = lifetimeScope.Resolve(); 我应该如何以及在何处将当前用户传递给我的记录器? 我是否应该假设WCF操作必须知道解析SomeBusinessObject需要首先解析IEventLogger并在解析SomeBusinessObject时传递已解析的实例? 这样的事情(原谅我,如果这不起作用,这只是一个想法): var […]

将Nininject MVC与类库一起使用

我对IoC框架很陌生,所以请原谅这个术语。 所以我所拥有的是一个带有Nininject MVC参考的MVC项目。 我的项目中有其他类libarys,例如Domain层,我希望能够在那里使用Ninject框架,但我的所有绑定都在MVC项目的App_Start文件夹下的App_Start中: private static void RegisterServices(IKernel kernel) { kernel.Bind().To(); kernel.Bind().To(); } 目前在我的类库中我使用构造函数注入,但有时我不得不对依赖项进行硬编码: var service = new WindowsHardwareService(); 当我希望能够做到以下几点: IKernel kernel = new StandardKernel(…..); var context = kernel.Get(); 我没有做以下因为我没有任何模块? 我读过的所有文档主要针对的是常规Ninject库而不是MVC版本。 我需要做什么,以及如何将常规Ninject库与MVC版本一起使用? 更新 这是我尝试过的: 这样做的目的是使每个项目都可以加载模块并获得当前注入的接口。 App_Start / NinjectWebCommon.cs (在MVC项目中) private static void RegisterServices(IKernel kernel) { var modules = new IoCModules(); var newKernal = modules.GetKernel(); kernel […]

如何为Unity中的所有已注册类型配置日志记录拦截器?

我将跟踪由Unity容器管理的服务中发生的每个操作作为文件系统中的日志。 我想我可以定义一个名为IService的接口,所有其他接口或实现都应该从他inheritance。 另一方面,我想开发一个自定义拦截行为或调用处理程序来将日志保存到文件中。 不幸的是,我发现使用此代码对我不起作用 IUnityContainer unity = new UnityContainer(); //Interception unity.AddNewExtension(); Interception interception = unity.Configure(); unity.RegisterType( new DefaultInterceptor(new InterfaceInterceptor()), new DefaultInterceptionBehavior(new LoggingBehavior())); string[] configFiles = Directory.GetFiles(“.”, “*.config”); foreach (string configFile in configFiles) { var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile }; System.Configuration.Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); var unitySection = (UnityConfigurationSection) configuration.GetSection(“unity”); unity = […]