2010-09-21 18 views
3

我正在创建通过Unity解析服务的新的instnace提供程序。 我不知道如何在web.config中添加配置。 以下是我的服务课。web.config应该如何在Unity依赖注入中用于WCF服务?

公共类服务: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; 
} 

}

回答

3

你应该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> 
+0

无需注册的服务类型。 – 2010-09-23 20:22:33

+0

正确;它需要一些其他容器,但不是Unity :) – 2010-09-24 06:55:50