autofac

Scope error when using Autofac with SignalR

為{幸葍}努か 提交于 2019-12-04 06:31:36
I'm trying to inject an HttpContextBase in my SignalR hub: public class EventHub : Hub, IDisconnect { private readonly HttpContextBase _httpContextBase; public EventHub(HttpContextBase httpContextBase) { _httpContextBase = httpContextBase; } [...] } The registration code looks like this: private static void InitAutofac() { var builder = new ContainerBuilder(); var assembly = typeof (MvcApplication).Assembly; builder.RegisterControllers(assembly).PropertiesAutowired(); builder.RegisterModule(new AutofacWebTypesModule()); builder.RegisterFilterProvider(); builder.RegisterAssemblyTypes(assembly)

Autofac with Open Generics and Type Specified at Runtime

回眸只為那壹抹淺笑 提交于 2019-12-04 06:06:26
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 flattener = (IFlattener)_container.Resolve(typeof(IFlattener<>), new TypedParameter(typeof(IFlattener<>),

Autofac get decorated QueryHandler by convention based on constructor parameter name?

柔情痞子 提交于 2019-12-04 05:39:35
问题 We inject IQueryHandler<TQUery,TResult> into our MVC controllers. We globally register all of these in the container We have written a decorator that can cache the results of IQueryHandler . We want to sometimes get cached reults and other times not from the same handler. Is it possible to conditionally get a decorated handler based on the name of the constructor parameter. e.g. inject IQueryHandler<UnemployedQuery, IEnumerable<People>> cachedPeopleHandler if we prefix constructor parameter

How to inject IHttpContextAccessor into Autofac TenantIdentificationStrategy

天涯浪子 提交于 2019-12-04 05:14:09
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 AssetClassIdentificationStrategy: ITenantIdentificationStrategy { private readonly IHttpContextAccessor

Castle Windsor Typed Factory Facility equivalents

一世执手 提交于 2019-12-04 05:12:42
do any other .NET IoC containers provide equivalent functionality to the typed factory facility in Castle Windsor? e.g. if I am using an abstract factory pattern in a WPF application: public class MyViewModel { private IAnotherViewModelFactory factory; public void ShowAnotherViewModel() { viewController.ShowView(factory.GetAnotherViewModel()); } } I don't want to have to create a manual implementation of IAnotherViewModelFactory for every type of ViewModel I wish to show, I want the container to take care of this for me. AutoFac has a feature called Delegate Factories , but as far as I can

JsonConverter Attribute : deserialize using custom constructor and Autofac

前提是你 提交于 2019-12-04 04:56:12
问题 Am using a custom JsonConverter to convert my JSON object. This is achieved via the JsonConverter attribute to the IQuery object below [JsonConverter(typeof(CustomConverter<IQuery>))] public interface IQuery { } The custom generic class is below (some bits removed for brevity) public class CustomConverter<T> : JsonConverter { // This should be created via AutoFac public ICustomObjectCreator<T> ObjectCreator { get; set; } // This default constructr always gets called public CustomConverter() {

Autofac difference between Register and RegisterType

江枫思渺然 提交于 2019-12-04 04:42:25
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(outputFilePath)).As<IOutputService>(); I don't understand why are we using a lambda expression to register this

Autofac, upgrade to version 4.0.0 is missing ConfigurationSettingsReader

天大地大妈咪最大 提交于 2019-12-04 04:19:15
问题 The latest documentation still refer's to the ConfigurationSettingsReader class which seems to be missing from the updated Autofac.Configuration assembly. How do get the equivalent of this code to function in version 4.0.0. I have this configuration: <section name="dependencies" type="Autofac.Configuration.SectionHandler, Autofac.Configuration" /> ... <dependencies> <modules> <module type="MyModule, MyAssembly.Service" /> </modules> </dependencies> and this code. // register the application

Autofac resolving a singleton creates a bottleneck

做~自己de王妃 提交于 2019-12-04 04:03:14
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 who depend on singletons and have slow constructors. These create a bottleneck for anyone else needing

WebApi, Autofac, System.Web.Http.Filters.ActionFilterAttribute Instance Per Request

五迷三道 提交于 2019-12-04 03:16:14
We have been using Autofac in our application (MVC 4 now) for a long time, we have dozens of attributes on the base controller everything inherits from and it has all been working fine so when the request begins our service is created and then available through all the attributes and on the controller action. We are now looking at WebApi and have created our WebApi controller and created an attribute on the base controller using the ActionFilterAttribute from the HTTP namespace. However the problem starts here where the service injected on the property on the attribute is not the same instance