autofac

Can I access the full power of Autofac in UnitTests, using the Moq integration

女生的网名这么多〃 提交于 2019-12-10 04:26:58
问题 My project (which happens built on top of Orchard, though I don't think that's relevant) uses Autofac. I am writing unit tests in which I want to stub out any dependencies using Moq, and I'm using the Autofac/Moq integration to achieve this. This is fine for any simple dependencies that are being passed in as constructor arguments. (Autofac documentation provides details of how to achieve this here). But because I don't ever have a containerBuilder, I don't see how to use a lot of Autofac's

Autofac 小试

怎甘沉沦 提交于 2019-12-10 04:24:10
ContainerBuilder builder = new ContainerBuilder(); ////builder.RegisterType<GetTest>().As<IBufrTest>(); //builder.RegisterType<GetTest>().AsImplementedInterfaces(); //builder.RegisterType<gettest2>().As<IBufrTest>(); string dllFIle = System.IO.Directory.GetCurrentDirectory() + "\\DemoBufr.dll"; builder.RegisterAssemblyTypes(Assembly.LoadFile(dllFIle)).AsImplementedInterfaces(); IContainer resorlver = builder.Build(); var test = resorlver.Resolve<IEnumerable<IBufrTest>>(); StringBuilder sbtext = new StringBuilder(); foreach (var item in test) { sbtext.Append(item.getstring()).Append("\r\n"); }

Caliburn.Micro + Autofac bootstrapping

主宰稳场 提交于 2019-12-09 18:33:39
问题 I have a project with Caliburn.Micro, and I'm trying to port from its SimpleContainer to Autofac. I'm using this code, that is an updated version of the code in this guide. Using SimpleContainer I simply did (inside the bootstrapper) protected override void OnStartup(object sender, System.Windows.StartupEventArgs e) { this.DisplayRootViewFor<IScreen>(); // where ShellViewModel : Screen } Now this doesn't work anymore, so what should I do to integrate Autofac with Caliburn.Micro ? 回答1: There

Autofac difference between Register and RegisterType

梦想与她 提交于 2019-12-09 17:25:47
问题 I have started to use Autofac following this tutorials: http://flexamusements.blogspot.com/2010/09/dependency-injection-part-3-making-our.html Simple class with no parameter in the constructor builder.RegisterType<ConsoleOutputService>().As<IOutputService>(); As explained in the tutorial, the code above can be read as: setup ConsoleOutputService as the implementation of IOutputService Simple class with one parameter in the constructor builder.Register(c => new MultipleOutputService

How to inject IHttpContextAccessor into Autofac TenantIdentificationStrategy

霸气de小男生 提交于 2019-12-09 17:23:37
问题 I am migrating my multitenant application from Webapi to aspnet core. In webapi version I was using TenantIdentificationStrategy that identified tenants based on request path on HttpContext . Moving to aspnet core, I am able to wire-up autofac successfully. I am not able to figure out how to wireup the tenant strategy. I tried injecting IHttpContextAccessor in ConfigureServices as services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); and my strategy looks like this public class

Autofac resolving a singleton creates a bottleneck

喜夏-厌秋 提交于 2019-12-09 17:05:03
问题 I'm using Autofac in an asp.net MVC app and came across a problem with locking. Anytime a service depends on a singleton, that service is resolved from the root lifetime scope. This is because Autofac: resolves singleton components from the root scope resolves any components from the root scope who have dependencies that must be resolved from root. Further, when resolving from any scope, Autofac locks that scope. I think these are fine design decisions. My problem is with misbehaving classes

Autofac with Open Generics and Type Specified at Runtime

穿精又带淫゛_ 提交于 2019-12-09 16:45:09
问题 The documentation states that Autofac supports open generics and I am able to register and resolve in a basic case like so: Registration: builder.RegisterGeneric(typeof(PassThroughFlattener<>)) .As(typeof(IFlattener<>)) .ContainerScoped(); Resolve: var flattener = _container.Resolve<IFlattener<Address>>(); The above code works just fine. However, assuming that I will not know the type provided to IFlattener until runtime, I want to do something like this: object input = new Address(); var

Autofac MultiTenant - how do I route to a subdomain?

孤街醉人 提交于 2019-12-09 14:14:21
问题 n00b here. Re-asking question because I didn't tag it right. I am trying to utilize Autofac's MutliTenant feature. I got an example "working" from the source files. I've scanned the docs and am having trouble figuring out how to "route" the tenants. Currently, I'd like to utilize a single code base for a basic CRUD app. The CRUD app will be used by several different sites, just focused on specific services for the individual site. I'm wanting to do this eventually: codebase.website1.com

FluentValidation Autofac ValidatorFactory

て烟熏妆下的殇ゞ 提交于 2019-12-09 11:18:53
问题 I need to be able to provide the IComponentContext to my ValidatorFactory to resolve FluentValidation Validators. I am a little stuck. ValidatorFactory public class ValidatorFactory : ValidatorFactoryBase { private readonly IComponentContext context; public ValidatorFactory(IComponentContext context) { this.context = context; } public override IValidator CreateInstance(Type validatorType) { return context.Resolve(validatorType) as IValidator; } } How do I provide the context and register the

Inject WebAPI UrlHelper into service using Autofac

可紊 提交于 2019-12-09 09:30:46
问题 I have a service used by a few controllers in my WebAPI project. The service needs to generate URLs, so ideally it would get a UrlHelper via a constructor parameter. public class MyService { public MyService(UrlHelper urlHelper) { ... } } I'm using Autofac as my IoC container. How can I register UrlHelper in the container? It needs a HttpRequestMessage and I can't figure out how to get the "current" message. 回答1: Use the RegisterHttpRequestMessage method to register the current request and