public void GetServiceReturnsNullForUnregisteredService()
        {
            var container = new ContainerBuilder().Build();
            var resolver = new AutofacWebApiDependencyResolver(container);

            var service = resolver.GetService(typeof(object));

            Assert.That(service, Is.Null);
        }
Esempio n. 2
0
 /// <summary>
 /// 构建服务并设置WebApi平台的Resolver
 /// </summary>
 /// <param name="services">服务映射信息集合</param>
 /// <param name="assemblies">要检索的程序集集合</param>
 /// <returns>服务提供者</returns>
 protected override IServiceProvider BuildAndSetResolver(IServiceCollection services, Assembly[] assemblies)
 {
     ContainerBuilder builder = new ContainerBuilder();
     builder.RegisterApiControllers(assemblies).AsSelf().PropertiesAutowired();
     builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
     builder.RegisterWebApiModelBinderProvider();
     builder.Populate(services);
     IContainer container = builder.Build();
     IDependencyResolver resolver = new AutofacWebApiDependencyResolver(container);
     GlobalConfiguration.Configuration.DependencyResolver = resolver;
     return (IServiceProvider)resolver.GetService(typeof(IServiceProvider));
 }