【问题标题】:How should the web.config look like in Unity Dependency Injection for WCF services?Web.config 在 Unity Dependency Injection for WCF 服务中的外观应该如何?
【发布时间】:2011-04-15 11:39:10
【问题描述】:

我正在创建通过 Unity 解析服务的新实例提供程序。 我不确定如何在 web.config 中添加配置。 以下是我的服务类。

公共类服务:IService {

private IUnitOfWork _unitOfWork; 

private IMyRepository _myRepository; 

// Dependency Injection enabled constructors 

public Service(IUnitOfWork uow, IMyRepository myRepository) 
{ 
    _unitOfWork = uow; 
    _myRepository = myRepository; 
} 

public bool DoWork()
{
        return true;
}

}

【问题讨论】:

    标签: wcf dependency-injection unity-container


    【解决方案1】:

    你应该only use web.config if you need to be able to vary the services after compilation。这不应被视为默认情况。

    这意味着在大多数情况下,最好使用代码作为配置,如下所示:

    container.RegisterType<Service>();
    container.RegisterType<IUnitOfWork, MyUnitOfWork>();
    container.RegisterType<IMyRepository, MyRepository>();
    

    如果您必须使用 XML 配置,您可以执行类似的操作。 Unity's excellent documentation explains the details.

    它可能会是这样的:

    <container>
      <register type="Service" />
      <register type="IUnitOfWork" mapTo="MyUnitOfWork" />
      <register type="IMyRepository" mapTo="MyRepository" />
    </container>
    

    【讨论】:

    • 无需注册Service类型。
    • 对;其他一些容器是必需的,但 Unity 不是:)
    猜你喜欢
    • 2017-08-14
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多