Autofac:如何将 IPrincipal 注入存储库层
本文关键字:注入 存储 IPrincipal Autofac | 更新日期: 2023-09-27 18:32:02
我正在使用 c#.net Web api 和 Autofac DI 容器使用存储库层/服务层/表示层设计一个 n 层应用程序。 这是我的困境。 我正在尝试对我的 Web api 控制器进行单元测试,但我的存储库对 IPrincipal 具有属性依赖性,我想将其属性注入到我的存储库层中。 我想创建一个模拟用户(IPrincipal)并将这个对象注入我的存储库。 这是我当前的层次结构,我的控制器是注入服务对象的构造函数,我的服务对象是注入了我的存储库对象的构造函数。 这部分似乎有效。 但是由于某种原因,每次运行测试时,我的 Principal 属性都是空的。 请查看下面的代码,让我知道我做错了什么:
Repository Base Class:
protected IPrincipal Principal
{
get { return _principal; }
}
Autofac Config Static Method
public class AutofacConfig
{
public static IContainer ConfigContainer()
{
var _builder = new ContainerBuilder();
UserPrincipal principal = MemberFactory.GetTestUser();
var _config = new HttpConfiguration();
_builder.RegisterControllers(typeof(BillingController).Assembly);
_builder.RegisterWebApiModelBinders(typeof(BillingController).Assembly);
_builder.RegisterApiControllers(typeof(BillingController).Assembly);
_builder.RegisterModelBinders(typeof(BillingController).Assembly);
_builder.RegisterModelBinderProvider();
_builder.RegisterModule(new AutofacWebTypesModule());
_builder.RegisterSource(new ViewRegistrationSource());
_builder.RegisterFilterProvider();
_builder.RegisterWebApiFilterProvider(_config);
//_builder.Register(x => principal).As<IPrincipal>().PropertiesAutowired();
_builder.RegisterType<BillingRepository>().As<IBillingRepository>().PropertiesAutowired();
_builder.RegisterType<UserPrincipal>().As<IPrincipal>().PropertiesAutowired();
_builder.RegisterType<GroupRepository>().As<IGroupRepository>().PropertiesAutowired();
_builder.RegisterType<BillingService>().As<IBillingService>().PropertiesAutowired();
_builder.RegisterType<UnitOfWork>().As<IUnitOfWork>();
_builder.Register(c => principal).As<IPrincipal>();
var _container = _builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(_container));
// Create the depenedency resolver.
var resolver = new AutofacWebApiDependencyResolver(_container);
// Configure Web API with the dependency resolver.
_config.DependencyResolver = resolver;
return _container;
}
}
Test Controller (Get Method)
[TestClass]
public class BillingControllerTest
{
[TestMethod]
public async Task Get()
{
var _container = AutofacConfig.ConfigContainer();
var _controller = _container.Resolve<BillingController>();
var _bodyCompRecords = await _controller.GetMyOutstandingBills(1, 10);
Assert.IsNull(_bodyCompRecords);
Assert.IsNull(_bodyCompRecords.BillingList);
Assert.IsNull(_bodyCompRecords.CurrentPage);
Assert.IsTrue(_bodyCompRecords.BillingList.Count > 0);
}
}
是否尝试为此属性添加受保护的集?我不确定 autofac,但也许你应该用 autoFac 知道这是一个可注入属性的一些属性来装饰此属性。
另一方面,.Net 应用程序中的所有线程都有一个主体。您可以在 Thread.CurrentPrincipal
静态属性上获取/设置它。您可以尝试设置自定义主体并使属性生成它。
在单元测试的设置中,您可以定义它,例如:
void Setup()
{
IPrincipal principal = new GenericPrincipal(new GenericIdentity("userName"), new string[] { "role1", "role2" });
Thread.CurrentPrincipal = principal;
}
在您的存储库中,您可以有一个属性来公开它,
protected IPrincipal Principal
{
get { return Thread.CurrentPrincipal; }
}
在 Web 应用程序(asp.net webforms/mvc/web api)中,您可以使用 HttpContext.Current.User
静态属性,因此,只需调用:
HttpContext.Current.User = principal;
作为一个好的实践,你可以同时设置HttpContext.Current.User
和Thread.CurrentPrincipal
。请记住,Web api 是无状态的,因此,实现一个 http 模块来从 http 标头读取参数,并将主体设置为应用程序。我不确定这是否是您的情况,但本文展示了如何做到这一点。
http://www.asp.net/web-api/overview/security/basic-authentication