我有一个内部构造函数的类,想要从Unity(2.0)解析它.
public class MyClass {
internal MyClass(IService service) {
}
}
Run Code Online (Sandbox Code Playgroud)
然后我在做
_container.Resolve<MyClass>();
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我有一个例外
Exception is: InvalidOperationException - The type MyClass cannot be constructed.
Run Code Online (Sandbox Code Playgroud)
IService已注册,唯一的问题是构造函数是内部的.我真的希望这个类是公开的,但是我希望它只能通过工厂(我实际上在调用它container.Resolve<MyClass>())中创建.
有没有办法让Unity看到内部构造函数?像InternalsVisibleTo或者什么?
我试图在我的代码中清理一些可访问性的东西,并无意中破坏了Unity依赖注入.过了一会儿,我意识到我标记了一些我不想在我的DLL之外暴露到内部的公共属性.然后我开始得到例外.
因此,似乎在Unity中使用[Dependency]属性仅适用于公共属性.我认为这是有道理的,因为内部和私人道具对于Unity程序集是不可见的,但是除了Unity之外,拥有一堆你永远不希望任何人设置或能够设置的公共属性感觉真的很脏.
有没有办法让团结也设置内部或私人财产?
这是我希望通过的单元测试.目前只有公共道具测试通过:
[TestFixture]
public class UnityFixture
{
[Test]
public void UnityCanSetPublicDependency()
{
UnityContainer container = new UnityContainer();
container.RegisterType<HasPublicDep, HasPublicDep>();
container.RegisterType<TheDep, TheDep>();
var i = container.Resolve<HasPublicDep>();
Assert.IsNotNull(i);
Assert.IsNotNull(i.dep);
}
[Test]
public void UnityCanSetInternalDependency()
{
UnityContainer container = new UnityContainer();
container.RegisterType<HasInternalDep, HasInternalDep>();
container.RegisterType<TheDep, TheDep>();
var i = container.Resolve<HasInternalDep>();
Assert.IsNotNull(i);
Assert.IsNotNull(i.dep);
}
[Test]
public void UnityCanSetPrivateDependency()
{
UnityContainer container = new UnityContainer();
container.RegisterType<HasPrivateDep, HasPrivateDep>();
container.RegisterType<TheDep, TheDep>();
var i = container.Resolve<HasPrivateDep>();
Assert.IsNotNull(i);
Assert.IsNotNull(i.depExposed);
}
}
public class HasPublicDep …Run Code Online (Sandbox Code Playgroud) 我想知道我应该如何存储/引用我的依赖注入容器.将容器作为静态类的静态属性是否可以?或者我应该将容器作为应用程序的实例变量吗?我想知道每个选项的优缺点是什么,以及在web,mvc,console和windows应用程序中最佳实践是什么?
structuremap dependency-injection castle-windsor inversion-of-control unity-container
我需要帮助.我使用Unity作为我的容器,我想在构造函数中注入两个相同类型的不同实例.
class Example
{
Example(IQueue receiveQueue, IQueue sendQueue) {}
}
Run Code Online (Sandbox Code Playgroud)
....和IQueue在我的MessageQueue类中实现....
class MessageQueue : IQueue
{
MessageQueue(string path) {}
}
Run Code Online (Sandbox Code Playgroud)
如何将两个不同的MessageQueue实例注入我的Example类?要使用不同的路径创建的每个MessageQueue实例.
用于Unity 3.0的Microsoft Unity Bootstrapper在此行上引发错误:
FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));
Run Code Online (Sandbox Code Playgroud)
出现以下错误:
mscorlib.dll中出现"System.ArrayTypeMismatchException"类型的异常,但未在用户代码中处理
附加信息:尝试将元素作为与阵列不兼容的类型进行访问.
整个代码都在这里,这是由nuget下载的bootstrapper预先制作和编写的.
Bootstrapper生成的文件App_Start/UnityMVCActivator.cs
using System.Linq;
using System.Web.Mvc;
using Microsoft.Practices.Unity.Mvc;
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(WebApplication.WebUI.App_Start.UnityWebActivator), "Start")]
namespace WebApplication.WebUI.App_Start
{
/// <summary>Provides the bootstrapping for integrating Unity with ASP.NET MVC.</summary>
public static class UnityWebActivator
{
/// <summary>Integrates Unity when the application starts.</summary>
public static void Start()
{
var container = UnityConfig.GetConfiguredContainer();
FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
// TODO: Uncomment if you want to use PerRequestLifetimeManager
// Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
}
}
}
Run Code Online (Sandbox Code Playgroud)
Bootstrapper生成的文件App_Start/UnityConfig.cs
using System;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration; …Run Code Online (Sandbox Code Playgroud) 我使用Unity作为我的IoC框架,我正在基于处理程序中每个请求的标头中的值创建一个类型:
var container = new UnityContainer();
container.RegisterType<IFoo,Foo>(new InjectionConstructor(valuefromHeader));
GlobalConfiguration.Configuration.DependencyResolver =
new Unity.WebApi.UnityDependencyResolver(container);
Run Code Online (Sandbox Code Playgroud)
问题是处理程序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
提前致谢.
我使用Unity App Block作为我的WOC项目服务层的IOC容器.使用Unity.WCF库将其插入每个WCF服务时,这非常有效.
我最近将RabbitMQ引入了我的服务层,我目前正在使用"使用"块来连接并添加到队列中.我不喜欢这个,我想用它HierachicalLifetimeManager来创建和销毁我与RabbitMQ的连接,因为我需要它们?这听起来不错吗?
我正在寻找一个这样的样本,或者至少是关于最佳方法的一些指导?(例如,我是否应该封装连接并根据需要注入每个服务?我将如何封装RabbitMQ使用者等?)
据我所知,Glass Mapper v4现在可以与任何IoC容器一起使用.但我很难找到如何实现这一目标的代码示例.
我希望能够注册玻璃组件并使用Unity将它们注入我的控制器,例如:
public class SearchController : Controller
{
private readonly ISitecoreContext _context;
//Inject via Unity
public SearchController(Glass.Mapper.Sc.ISitecoreContext context)
{
_context = context;
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以提供一个代码示例,说明如何将Glass与Unity连接起来?
我是Repository和DI的新手,并试图在我的MVC 5项目中实现.
我实现了构造函数注入,在我的控制器中有一个像这样的构造函数:
IBook _ibook;
public Test(IBook ibook)
{
_ibook = ibook;
}
Run Code Online (Sandbox Code Playgroud)
没有任何DI库,它会抛出一个错误:没有空构造函数.
为了避免这种情况,我又添加了一个构造函数,如下所示:
public Test ():this(new Book())
{
}
Run Code Online (Sandbox Code Playgroud)
由于我是DI的新手,我不想通过使用DI库冒险我的项目,后来可能会抛出一些我可能无法解决的错误.
我想知道如果我不使用DI库会遇到什么问题.
如果推荐,哪个DI库适合初学者?我见过很少有NInject和Unity的视频.
asp.net-mvc design-patterns dependency-injection ninject unity-container
我有一个MVC应用程序,它使用Unity作为其IoC容器,并使用我的应用程序在我的应用程序中定义了多个服务PerRequestLifetimeManager.
container.RegisterType<IFileService, FileService>();
一切正常,除非我尝试将我的解决方案推广到自动化任务(如SharePoint TimerJobs),并以不同的时间间隔启动.
为此,我在一个单独的项目中定义了一个ServiceLocator-Type类ContainerManager,基本上就是这样:
public static object Resolve(string typeName)
{
var type = Type.GetType(typeName);
return Resolve(type);
}
public static object Resolve(Type type)
{
object result = DependencyResolver.Current.GetService(type);
return result;
}
public static T Resolve<T>() where T : class
{
object result = DependencyResolver.Current.GetService<T>();
return (T)result;
}
public static object Resolve(string typeName)
{
var type = Type.GetType(typeName);
return Resolve(type);
}
public static object Resolve(Type type)
{
object result = DependencyResolver.Current.GetService(type);
return result;
} …Run Code Online (Sandbox Code Playgroud) unity-container ×10
c# ×5
.net ×2
asp.net-mvc ×2
glass-mapper ×1
ninject ×1
rabbitmq ×1
sitecore ×1
structuremap ×1