Tag: unity container

我可以像在Autofac中那样在Unity中的模块中注册我的类型吗?

我对Autofac非常熟悉,我非常喜欢Autofac的一个function是注册模块。 有谁知道我怎么能用Unity做到这一点? 如果有的话,我很难找到在Google中使用哪些术语来提出等效的统一。 public class Global : HttpApplication, IContainerProviderAccessor { private static IContainerProvider _containerProvider; protected void Application_Start(object sender, EventArgs e) { var builder = new ContainerBuilder(); builder.RegisterModule(new MyWebModule()); _containerProvider = new ContainerProvider(builder.Build()); } […] public IContainerProvider ContainerProvider { get { return _containerProvider; } } } public class MyWebModule: Module { protected override void Load(ContainerBuilder builder) […]

在Web API中使用Handler并根据请求解析Unity

我使用Unity作为我的IoC框架,我正在基于处理程序中每个请求的标头中的值创建一个类型: var container = new UnityContainer(); container.RegisterType(new InjectionConstructor(valuefromHeader)); GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container); 问题是处理程序的SendAsync意味着全局容器被不同的请求覆盖,并且在构造函数中使用IFoo的控制器获取的值不正确。 1)我可以使SendAsync同步吗? 2)如果没有,如何为每个请求创建不同的实例并让IoC容器安全地解析? 我查看了以下文章但没有成功: http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver http://www.strathweb.com/2012/11/asp-net-web-api -and-dependencies-in-request-scope / http://benfoster.io/blog/per-request-dependencies-in-aspnet-web-api-using-structuremap 提前致谢。

在WPF中使用Unity解析时,SynchronizationContext.Current为null

我有一个看起来像这样的WPF代码。 public class AlphaProductesVM : BaseModel { private ObservableCollection _NwCustomers; private int i = 0; public AlphaProductesVM () { _NwCustomers = new ObservableCollection(); var repository = new NorthwindRepository(); repository .GetAllProducts() .ObserveOn(SynchronizationContext.Current) .Subscribe(AddElement); } public void AddElements(IEnumerable elements) { foreach (var alphabeticalListOfProduct in elements) { AddElement(alphabeticalListOfProduct); } } public ObservableCollection NwCustomers { get { return _NwCustomers; […]

我是否应该使用OwinContext的环境来保存每个请求的特定于应用程序的数据

我需要一种方法来存储每个请求的日志对象。 使用HttpContext,我会将其添加到项目Dictionary中。 如果我能帮助它,我不想把HttpContext带进去。 下面的代码是我建议的Unity LifeTimeManager,它将在OwinContext的Environment属性中存储对象,我可以使用我的Owin中间件访问它。 public class OwinContextLifetimeManager : LifetimeManager { private string key = (new Guid()).ToString(); private IDictionary environment; public OwinContextLifetimeManager(IDictionary environment) { this.environment = environment; } public override object GetValue() { if (environment != null && environment.ContainsKey(key)) return environment[key]; else return null; } public override void RemoveValue() { if (environment != null) environment.Remove(key); […]

使用Unity与最小配置

在工作中,我们使用Unity很多。 它的function非常棒,但您使用的越多,配置文件越多,运行时问题就越多,您为每个测试项目重新创建统一配置的次数就越多。 所以我们最终得到一个巨大的统一配置部分,必须在几个项目中复制,当需要部署时,你最终必须追踪你忘记添加引用的dll,但是你只能在运行时发现它们。 不好玩。 我猜测有人遇到过这个问题而且有一个解决方案。 理想情况下,我想弄清楚如何以一种使用约定优于配置的方式配置Unity并减少运行时问题(即,巨大的配置文件)。 有人知道用最小配置实现Unity的好方法吗? 编辑:有一点:我必须坚持只使用Unity。 无法真正切换到Ninject等

Unity DI使用PerRequestLifetimeManager注入DbContext

我有以下代码用Unity初始化实例: IUnityContainer container = new UnityContainer(); container.RegisterType(new PerRequestLifetimeManager(), new InjectionConstructor()); container.RegisterType(typeof(IGenericRepository), typeof(GenericRepository)); container.RegisterType(new PerRequestLifetimeManager()); container.RegisterTypes( AllClasses.FromAssemblies( Assembly.GetAssembly(typeof(IUserService)), Assembly.GetAssembly(typeof(UserService))), WithMappings.FromMatchingInterface, WithName.Default, WithLifetime.PerResolve); DependencyResolver.SetResolver(new Unity.Mvc4.UnityDependencyResolver(container)); GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container); 我使用PerRequestLifetimeManager所以我按照MSDN上的建议,并在上面代码的末尾添加了新行: DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule)); 但在我放置之后。 当加载页面(仅静态html)时,我向我的WebApi控制器发送ajax请求,该控制器调用抛出错误的GenericReposirory Get()方法: The operation cannot be completed because the DbContext has been disposed. 没有这行代码,一切正常,但没有设置它可能不会处置上下文。 我的UnitOfWork类: public class UnitOfWork : IUnitOfWork, IDisposable { private readonly VotingSystemContext […]

System.Core.dll中发生了未处理的“System.StackOverflowException”类型exception

在我的Asp.net MVC项目中 我有一个初始化统一容器的bootsrapper。 我不知道为什么,但我明白了 System.Core.dll中发生了未处理的“System.StackOverflowException”类型exception 我已经加倍检查,只在我的初始化程序中完成注册。 所有依赖项仅在ctors中注入。 有什么可能导致这个? protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); Initializer.Initialize(); BundleConfig.RegisterBundles(BundleTable.Bundles); } 它在BundleConfig.RegisterBundles(BundleTable.Bundles);之后失败BundleConfig.RegisterBundles(BundleTable.Bundles); public static class Initializer { private static bool isInitialize; private static readonly object LockObj = new object(); private static IUnityContainer defaultContainer = new UnityContainer(); static Initializer() { Initialize(); } public static void Initialize() { if […]

当需要多个相同类型的实例时,使用Unity进行DI

我需要帮助。 我使用Unity作为我的容器,我想在构造函数中注入两个相同类型的不同实例。 class Example { Example(IQueue receiveQueue, IQueue sendQueue) {} } ….和IQueue在我的MessageQueue类中实现…. class MessageQueue : IQueue { MessageQueue(string path) {} } 如何将两个不同的MessageQueue实例注入我的Example类? 要使用不同路径创建的每个MessageQueue实例。

如何使用Unity解决静态类中的依赖关系?

我有以下扩展方法,它(自然地)存在于静态类中。 public static class MyExtensions { [Dependency] private static IMyDataContext _myDataContext { get; set; } public static void MyExtensionMethod(this MyType myType) { // do stuff _myDataContext.DoAwesomeThing(); } } _myDataContext对象为null 。 通常我会使用UnityContainer来注册类型,但由于这是一个静态类,我不能。 我需要什么来实例化_ myDataContext以便在需要时它不为空?

Unity和ASP.NET WebForms – 没有为此对象定义的无参数构造函数

有没有人有任何关于如何使Unity 1.2或2.0与ASP.NET WebForms一起工作的好例子? 我以为我弄明白了,但显然我错过了一些东西。 现在我收到了错误; “没有为此对象定义无参数构造函数”。 我记得几年前收到这个错误,我只是不记得我做了什么。 显然Unity并没有按照它应有的方式运作,因为我忘记了某些事情。 任何帮助,将不胜感激。 这是我的一些代码: Global.asax中 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Web.Security; 使用System.Web.SessionState; 使用Microsoft.Practices.Unity; 使用PIA35.Unity; 命名空间PIA35.Web { public class Global:System.Web.HttpApplication { protected void Application_Start(object sender,EventArgs e) { IUnityContainer container = Application.GetContainer(); PIA35.Web.IoC.Bootstrapper.Configure(容器); } } } 这是web.config文件的httpModules部分: 这是我的IoC bootstrapper类的代码。 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用Microsoft.Practices.Unity; 使用PIA35.Services.Interfaces; 使用PIA35.Services; 使用PIA35.DataObjects.Interfaces; 使用PIA35.DataObjects.SqlServer; 命名空间PIA35.Web.IoC { public static […]