标签: unity-container

IoC在类库中.在哪里引导

我正在使用可以被其他组件重用的类库.在这个类库中,我使用unity来进行依赖注入.对于这个类库,我创建了一个测试项目.调用者也获得了一个测试项目.我不确定的一件事是绑定的位置.我应该在类库中加入它还是应该从调用应用程序中执行此操作?

c# dependency-injection inversion-of-control unity-container

30
推荐指数
2
解决办法
2万
查看次数

RegisterType()调用中的InjectionMembers是什么?

我一直在使用Microsoft的Unity IOC容器.RegisterType()所有类似的方法都有一堆重载

IUnityContainer RegisterType(Type t, params InjectionMember[] injectionMembers);
Run Code Online (Sandbox Code Playgroud)

我想知道injectionMembers参数的用途是什么时候?我找不到任何关于它们的文档(即使它们在每次重载时都没有),我看过的示例代码都没有使用它们.

我在这里错过了什么吗?它们不常用或者我只是错过了这些例子吗?

c# unity-container

29
推荐指数
1
解决办法
2万
查看次数

使用Unity的ASP.NET MVC中的基本控制器构造函数注入

我的MVC 5项目中有一个基本控制器,它实现了一些共享功能.此功能需要一些依赖项.我正在使用Unity 3将这些实现注入到我的控制器中,这种模式运行良好,直到我将控制器切换为从该基本控制器继承.现在我遇到了以下问题:

public class BaseController : Controller
{
    private readonly IService _service;

    public BaseController(IService service)
    {
        _service = service;
    }
}

public class ChildController : BaseController
{
    private readonly IDifferentService _differentService;

    public ChildController(IDifferentService differentService)
    {
        _differentService = differentService;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是可以理解的抛出错误'BaseController' does not contain a constructor that takes 0 arguments.Unity没有解析BaseController的构造,因此它无法将依赖项注入其中.我看到了解决这个问题的两种明显方法:

1.)显式调用BaseController ctor并让每个ChildController ctor注入BaseController的依赖项

public class ChildController : BaseController
{
    private readonly IDifferentService _differentService;

    public ChildController(IDifferentService differentService,
                           IService baseService)
        : base(baseService)
    {
        _differentService = differentService;
    }
}
Run Code Online (Sandbox Code Playgroud)

我不喜欢这种方法有几个原因:一,因为ChildControllers没有使用额外的依赖项(因此它会导致子控制器中的构造函数膨胀),更重要的是,如果我更改了构造函数签名基本控制器,我必须更改每个子控制器的构造函数签名. …

c# asp.net-mvc dependency-injection inversion-of-control unity-container

27
推荐指数
2
解决办法
3万
查看次数

Web Api使用IDependencyResolver实现启动异常

我正在开发Web Api,我决定使用自定义DependencyResolver.我引用了[Web API控制器的依赖注入]一文.到目前为止,依赖注入控制器的所有方面都运行良好.我的Owin启动类的配置代码片段

private void RegisterIoC(HttpConfiguration config)
{
    _unityContainer = new UnityContainer();
    _unityContainer.RegisterType<IAccountService, AccountService>();
    .........
    .........
    config.DependencyResolver = new UnityResolver(_unityContainer);
}
Run Code Online (Sandbox Code Playgroud)

但是当Api第一次启动时,UnityResolver的 GetService方法中抛出(但捕获)一些ResolutionFailedException .这是异常消息

"Exception occurred while: while resolving. 
Exception is: InvalidOperationException - 
The current type, System.Web.Http.Hosting.IHostBufferPolicySelector, 
**is an interface and cannot be constructed. Are you missing a type mapping?**"
Run Code Online (Sandbox Code Playgroud)

上述同样的异常抛出以下几种类型

System.Web.Http.Hosting.IHostBufferPolicySelector
System.Web.Http.Tracing.ITraceWriter
System.Web.Http.Metadata.ModelMetadataProvider
System.Web.Http.Tracing.ITraceManager
System.Web.Http.Dispatcher.IHttpControllerSelector
System.Web.Http.Dispatcher.IAssembliesResolver
System.Web.Http.Dispatcher.IHttpControllerTypeResolver
System.Web.Http.Controllers.IHttpActionSelector
System.Web.Http.Controllers.IActionValueBinder
System.Web.Http.Validation.IBodyModelValidator
System.Net.Http.Formatting.IContentNegotiator
Run Code Online (Sandbox Code Playgroud)

我知道抛出这些ResolutionFailedException是因为我没有在上面的类型的单位配置中提供映射.

现在这里是我的问题: -,如果我实现自定义统一DependencyResolver我需要定义上述类型的映射,如果需要定义它们对应的默认实现类型,或者是否有一些替代方法来实现DependencyResolver.我真的担心即使应用程序运行正常,但未能解决上述类型可能会导致严重问题. …

dependency-injection unity-container asp.net-web-api dependency-resolver

27
推荐指数
5
解决办法
2万
查看次数

IOC容器的最佳实践

我正在使用Unity IOC容器,我只是想知道访问多个类的容器的最佳方法是什么.

每个类都应该有一个IUnityContainer成员,然后通过构造函数传递容器吗?是否应该有一个带有IOC容器的单例类?

asp.net开发怎么样?

有人可以指导我朝正确的方向发展吗?谢谢.

c# inversion-of-control unity-container

26
推荐指数
3
解决办法
8545
查看次数

Unity Singleton代码

我是Unity的新手,我正在尝试编写一些Unity逻辑,它初始化并注册/解析Email对象的单例实例,以便它可以用于其他几个对象,下面的一个例子是OperationEntity.

因此,当它注册时,它会使用配置文件中的某些值填充Email单例,然后每当创建一个OperationEntity实例时(在我的情况下它被反序列化)它就会使用相同的Email单例.所以我的所有客户端逻辑需要做的是反序列化OperationEntity并调用PerformAction() - 使用Unity负责的电子邮件实例.

public interface IEmail
{
    string FromName { get; set; }
    string FromEmailAddress { get; set; }
}

public class Email : IEmail
{
    public string FromName { get; set; }
    public string FromEmailAddress { get; set; }

    public Email(string fromName, string fromEmailAddress)
    {
        FromName = fromName;
        FromEmailAddress = fromEmailAddress;
    }
}

public class OperationEntity
{
    private readonly IEmail _email;

    public int OperationId { get; set; }
    public string OperationName { get; set; }
    public …
Run Code Online (Sandbox Code Playgroud)

c# unity-container

26
推荐指数
3
解决办法
5万
查看次数

无法加载文件或程序集System.Web.WebPages.Razor ,, Version = 3.0.0.0或其依赖项之一

我在我的应用程序中使用MVC 5,WCF和Unity框架.

运行WCF服务时出现以下错误:

Server Error in '/' Application.

Could not load file or assembly 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc wcf inversion-of-control unity-container

26
推荐指数
1
解决办法
6万
查看次数

为什么在指定生命周期管理器时注册了两次类型?

我在以下场景中使用Unity的会议注册机制:

public interface IInterface { }

public class Implementation : IInterface { }
Run Code Online (Sandbox Code Playgroud)

给定Implementation类及其接口我正在RegisterTypes以下列方式运行:

unityContainer.RegisterTypes(
    new[] { typeof(Implementation) },
    WithMappings.FromAllInterfaces,
    WithName.Default,
    WithLifetime.ContainerControlled);
Run Code Online (Sandbox Code Playgroud)

此次通话后,unitContainer包含三个注册:

  • IUnityContainer- > IUnityContainer (好的)
  • IInterface- > Implementation(好的)
  • Implementation- > Implementation(???)

当我按如下方式更改呼叫时:

unityContainer.RegisterTypes(
    new[] { typeof(Implementation) },
    WithMappings.FromAllInterfaces,
    WithName.Default);
Run Code Online (Sandbox Code Playgroud)

容器只包含两个注册:

  • IUnityContainer- > IUnityContainer(好的)
  • IInterface- > Implementation(好的)

(这是期望的行为).

在窥探Unity的源代码之后,我注意到对于IUnityContainer.RegisterType应该如何工作存在一些误解.

RegisterTypes方法的工作原理如下(注释表明上述场景中的值是什么):

foreach (var type in types)
{ …
Run Code Online (Sandbox Code Playgroud)

.net c# unity-container

26
推荐指数
2
解决办法
998
查看次数

在Unity中需要处理?

可能是一个Unity初学者的问题:当使用Unity时,你还需要在你注入的对象上实现Dispose方法吗?或者甚至不需要这样(因此,由Unity自动完成)?这是在Web应用程序的上下文中.

dispose unity-container

25
推荐指数
2
解决办法
2万
查看次数

无法摆脱"物理连接不可用"的例外

我即将开枪自杀.花了几周时间试图解决这个问题.我们有一个ASP.NET MVC 4 Web应用程序,它使用SQL Server 2012和Entity Framework作为ORM和Unity for IoC.Web应用程序托管在Amazon EC2上.我开始得到"物理连接不可用"的例外.它每天发生几次.我搜索了很多文章和论坛,并尝试了所有可能的建议:

  • 尝试从连接字符串"Polling = False"中删除池
  • 尝试限制池大小和连接生命周期
  • 尝试将Unity的LifetimeManager更改为HierarchicalLifetimeManager,PerRequestLifetimeManager.还确保在请求结束后处理实体上下文
  • 删除了所有TransactionScope引用

当异常发生时,恢复应用程序的唯一方法是重启服务器,这是非常糟糕的!

这是完全例外:

将请求发送到服务器时发生传输级错误.(提供者:会话提供者,错误:19 - 物理连接不可用)

sql-server asp.net-mvc entity-framework ioc-container unity-container

25
推荐指数
3
解决办法
5万
查看次数