Questions tagged [autofac]

1

votes
1

answer
187

Views

Autofac. Naming resolving and child dependencies

Does the named/keyed resolving also resolve child dependencies using a name? Or named resolving applies Name/Key only for resolved type without its child dependencies? Error occurs: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'ConsoleAppl...
1

votes
0

answer
862

Views

Executing MediatR PreProcessor Only for Specific Interface Types (Commands)

[NOTE: This is a 'replacement' question. The first one was based on my main project's code so I've redone the question with code from a single-purpose project that illustrates the principle more cleanly. The question remains the same, just better presented.] The Scenario I'm trying to setup a comman...
platypusjh
1

votes
0

answer
265

Views

Multiple notifications with MediatR and Autofac ContravariantRegistrationSource

I am trying to use MediatR to set up generic notifications, but the notification handler is being called multiple times. https://github.com/smartaypants/MediatR/blob/master/test/MediatR.Tests/CustomNotificationTests.cs The test publishes a CustomNotification which implements ICustomNotification. The...
smartypants
1

votes
1

answer
231

Views

Referencing .NET 4.7.1 library using Azure Storage from ASP.NET Core 2 MVC App

In my solution I have an ASP.NET Core 2 MVC app using Razor pages, a Web API 2 app and a .NET 4.7.1 class library containing services and their definitions that use Azure Table Storage from the Azure Storage NuGet (v8.7.0). I'm using autofac for dependency injection. I have hooked up both my web app...
James Mundy
1

votes
0

answer
132

Views

Registering and consuming single instance with various dependencies with Autofac in ASP.NET application

I want to have a single instance of WorkerContainer class that injects various worker implementations. I want to get this instance in Global.asax and start workers. public class WorkerContainer { IWorker[] _workers; public WorkerContainer(IWorker[] workers)//DI { _workers = workers; } public void St...
vpetrovic
1

votes
2

answer
110

Views

Common code for dependency injection for web api, admin panel and mvc controller

Currently, I have below 4 projects in my solution file : API (Web API) Web (MVC) Admin (MVC) Service Layer (C# Library) The service layer is being used by all the 3 web projects. The service is injected using Autofac container. The services are registered in each of the web projects during startup w...
user3151766
1

votes
0

answer
135

Views

Wiring up Autofac in MonoGame

I'm attempting to wire up Autofac v4.6.2 in MonoGame v3.6. This is what I have in my program.cs public static class Program { private static IContainer Container { get; set; } [STAThread] static void Main() { var builder = new ContainerBuilder(); builder.RegisterType().InstancePerLifetimeScope().Pro...
John Geddes
1

votes
1

answer
115

Views

CORS vs Autofac vs Exception handling middleware in Owin, which one goes first?

I have an owin setup that among many other things, uses CORS and Autofac. The Autofac documentation says 'Register the Autofac middleware FIRST.', many people say app.UseCors should be the very first thing. I also have an exception handling middleware, which many people also say should be the first...
BlackTigerX
1

votes
0

answer
39

Views

Log Exceptions with EntityFramework in WebAPI Exception Filter or IExceptionLogger

I'm thinking to use ExceptionLogger or ExceptionFilterAttribute in my Web API 2.x application. My OR/M is Entity Framework 6 and AutoFac as my IoC. Consider exception attribute below: public class ApiExceptionFilterAttribute : ExceptionFilterAttribute { public IDbContext MyContext { get; set; } pub...
Ali Bahraminezhad
1

votes
0

answer
91

Views

autofac registration in webapi some singleton components some per request

Our framework has the following structure: webapi project basic CQRS project Nhibnerate configuration project The solution uses Autofac to wire up all the classes. The api will load all assemblies and register all the modules in those assemblies. Most of the modules simply look for all interfaces a...
Jon
1

votes
1

answer
237

Views

Autofac Web API error Make sure that the controller has a parameterless public constructor

I am getting this error while configuring Autofac with ASP.NET WebAPI. An error occurred when trying to create a controller of type 'UserController'. Make sure that the controller has a parameterless public constructor. Startup.cs public partial class Startup { public void Configuration(IAppBuilder...
Ali Shahzad
1

votes
0

answer
59

Views

Why does Autofac.config fail to load in [ClassInitialize] when running all tests?

I have a class initialize method that runs sets up an autofac.config amoung other dependencies in mstest. When I run the tests in the file individually they pass. however when I run the tests together I get the following error: 'The configuration file 'autofac.config' was not found and is not option...
SlipperyBalmain
1

votes
1

answer
117

Views

Registering instances from property of other registered types?

Is there a better way to code this dependency on Configuration, how can I register an object which is provided by a property of another registered object? I have a configuration object which exposes a configuration property for each service. I'd like to initialize those services based on their confi...
JohnKoz
1

votes
1

answer
88

Views

Unable to register service

I'm trying to register a service using Prism autofac. However, I cannot seem to figure out a way to make this work. Everything I find on the internet is for older versions of prism and does not really show me how to use it now. protected override void RegisterTypes(IContainerRegistry containerRegis...
Bjorn Fontaine
1

votes
0

answer
98

Views

Autofac - Register Generic Type with lambda expression

I am trying to register IDbSet as a dependency for my logic classes. Is there a way to achieve something like this? builder.Register(c => { var context = myContainer.Resolve(); return context.Set();// Problem Here }).As(typeof(System.Data.Entity.IDbSet)); Or suggest an alternate way to access my...
Mohamed Atia
1

votes
1

answer
316

Views

DbContext is Disposed When Using Autofac Dependency Injection on WebApi project

I have a WebApi project using Entity Framework 6.0, Autfac for DI and CQRS architecture. The problem I have that DbContext isn't disposing how it supposed to. The action I take: I run two quick requests, e.g. send request from Postman to one endpoint, runtime stops on breakpoint in controller method...
lukasz
1

votes
0

answer
211

Views

Can't Enqueue my Job in hangfire server

I am working on enqueue jobs using hangfire in my application. my enqueue job statement is given below, string jobId = BackgroundJob.Enqueue(() => strategy.get(typeof(_service.Engine.Summary), cpdata)); when this will enqueue my job successfully but got the error message in State table like this, '...
npatel
1

votes
2

answer
131

Views

Autofac Wcf - Inject service depending on data within SOAP Request

I have a WCF Service with the following operation contract: [OperationContract] Response SearchEntities(Query query); This operation takes a request that contains a specified Entity like so: [DataContract] public class Query { [DataMember] public string SearchTerm { get; set; } [DataMember] public s...
Andy Clark
1

votes
1

answer
74

Views

Easy way to pass primitive value to ApiController using AutoFac

Given the following method of configuring Autofac: public class DefaultConfigurationModule : Module { protected override void Load(ContainerBuilder builder) { builder.RegisterApiControllers(System.Reflection.Assembly.GetExecutingAssembly()); } } And given the following Web-Api controller: public cla...
RobIII
1

votes
1

answer
142

Views

Service Fabric Autofac how to?

I'm trying to configure IoC (concept I'm not very familiar with yet) in my SF in a stateful service as explained here : https://www.codeproject.com/Articles/1217885/Azure-Service-Fabric-demo and here : https://alexmg.com/posts/introducing-the-autofac-integration-for-service-fabric. in program.cs - m...
Xav Sc
1

votes
1

answer
746

Views

Autofac.Core.DependencyResolutionException but everything is registered

I have this portion of code: var _containerBuilder = new ContainerBuilder(); _containerBuilder.RegisterType().As(); var container = _containerBuilder.Build(); using (var scope = container.BeginLifetimeScope()) { var navigationService = scope.Resolve(); } This is throwing me a runtime exception when...
Zeppelin
1

votes
0

answer
32

Views

AutoFac equivalent of this ninject command/function

In Ninject i can configure a container like the mentioned below, What is the Autofac equivalent for the 'Highlighted ones'? Currently i am trying to achieve the same using AutoFac, any help is much appreciated. public class NinjectAdapter : IContainerAdapter { private readonly IKernel _kernel; publi...
Cody
1

votes
1

answer
309

Views

Is it possible to use Autofac somehow within Unity3d?

I am using Unity3d 2018.1.0f3 which is using .net 4.6x and has netstandard 2.0. Does anyone know if its possible to get Autofac working within Unity? Currently, I get an error saying that it unloaded the assemblies due to possible runtime issues. Thanks, MH
MostHated
1

votes
0

answer
58

Views

Asp.NET MVC Autofac Inject Dependence non-controller class

All classes that inherit from controller have already inserted the dependencies, but I do not know how to inject those who do not inherit from contoller. in the example below the SendEmailAndLog() method waits for the ILoggerService interface to be injected. but I do not know how to do this. public...
Paulo Jardim
1

votes
1

answer
212

Views

Register and Retrieving Multiple Connection String using Autofac on .net core

When I register 2 connection string on autofac, i only retrieve the last registered connection string using keyfilter on IDbConnection. //Register builder.RegisterType() .As(IDbConnection).... .Named('conn1') //Retrieve Public Repository([KeyFilter]IDbConnection connection)....
noeled
1

votes
1

answer
40

Views

How to access parent object's instance when using property injection

I am trying to add Autofac into my Webforms application (MVP pattern). I have a problem with property injection. At the moment the Presenter property is instantiated with a brand new instance of the View class. That causes a null reference exception when 'view.Text' is set inside Presenter's constr...
Daniel
1

votes
1

answer
98

Views

Error with Autofac in Xamarin Android App

I am using Autofac in a Xamarin project to build a Mobile App. I get the following error when building an Autofac container, in Xamarin Live Player 1.5.176 (676) (on a Huawei P20, last security patch) : [LogEntry: Time=01/07/2018 11:00:08 +02:00, Level=Error, Title=Visualization Error, Message=No b...
Rom Eh
1

votes
1

answer
117

Views

Share instance between AutoFac containers

I have an issue where I have a stateless service which has 2 AutoFac containers I have a PriceService instance inside my container that is created the Main method of Program.cs A second container is created inside my Startup.cs (unavoidable because different logic is required for setting up AutoFac...
Paul
1

votes
1

answer
502

Views

Autofac System.ArgumentNullException : Value cannot be null. Parameter name: context

I created autofac instance per matching lifetime scope and create child scope if parent scope already exist but i got exception. Please see the code and stack trace below. Code public static class App { private static AsyncLocal _upperScope; public static AsyncLocal Number = new AsyncLocal(); public...
Muhammad Ali
1

votes
1

answer
88

Views

DI with UnitOfWork and Repository patterns in WindowsForm use Autofac

I have problem when I build project WindowsForm application Dependency Injection. Here this is my code in Program.cs file. var builder = new ContainerBuilder(); builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()); // Register your Web API controllers. //builder.RegisterApiControllers(Ass...
ltiendat95
1

votes
3

answer
56

Views

Autofac variable name based injection

I want to inject some ValueType variable values using autofac, as we do same for Interfaces. I don't want to additionally mention in bootstrapper that this class have named parameterized injection or with Key. For eg: This is easily injectable Registration => builder.RegisterType().As(); Injection...
Trikaldarshi
1

votes
1

answer
98

Views

Autofac - Singleton interface resolved as transient

I'm building a WPF application and using Autofac as an IoC container. The scenario is kind of hard to describe but I'll try to give the relevant piece of code below: My app bootstrapper contains the following code to register the viewmodels in my app: builder.RegisterAssemblyTypes(assemblySource)...
Wahib Mkadmi
1

votes
0

answer
64

Views

Bot Framework AutoFac Dependency Issue C# (Multiple Container)

In my app start I have the following code to register my module. Conversation.UpdateContainer(builder => { builder.RegisterModule(); }); And I am using InstrumentedLUIS which has the following code. var builder = new ContainerBuilder(); builder.Register(c => new BotFrameworkApplicationInsightsInstru...
LM1015
1

votes
1

answer
212

Views

Options<T> not populating in DI

I'm using .Net Core 2.1 and an Aggregate / Facade pattern for my dependencies (which I happily do elsewhere using Ninject / .net 4.6). But when I try to pass through options I get a null (Debugging I can see there being picked up) but there not passed to Autofac (I'm fairly sure its my as they weren...
Scrambledheads
1

votes
0

answer
132

Views

Correct way to use Autofac with Factory Pattern?

IN a Winforms project, I have set up Autofac and Factory Pattern and it looks like it's working as expected. However, I'm still not sure whether the following is best practice. The factory class is: public static class Client { public static readonly IRequestFactory RequestFactory = new RequestFacto...
1

votes
0

answer
80

Views

Nancy Autofac prevent container dispose

Is there any possibility to prevent Nancy from rearranging container with new requests? I've created this simplyfied code to show my problem. Constructor for ConsoleWriter is called every single time and don't want this to happen. Of course I could make ConsoleWriter a singleton, but my real-life pr...
KwarcPL
1

votes
1

answer
42

Views

Using Autofac Ioc InstancePerHttpRequest with Servicestack 5.1.0

We have recently upgraded our API's (using Forms Authentication) Servicestack version 4.0.40 to latest stable version of Servicestack (V5.1.0). We use Autofac Ioc registration using InstancePerHttpRequest for API's (this is using Autofac.Integration.Mvc from Autofac.Mvc4 package). Below code does n...
Kalyan
1

votes
1

answer
42

Views

Autofac independent scope when resolving IEnumerable<Service>

I have a collection of services which are resolved like this: //other scope, such as default per http request in aspnet core // new scope is created which is not part of web request scope using (var scope = container.BeginLifetimeScope()) { var services = scope.Resolve(); } However the problem is th...
SHM
1

votes
0

answer
138

Views

C# AutoFac Inject Without Http Context (Outside Controller)

My class Is waiting for a Interface but I need initialize a class from a job in the Application_Start. protected void Application_Start() { Bootstrapper.Run(); JobScheduler.RunJobSchedule(); } On Bootstrap.Run() I have the container builder. like: private static void SetAutofacContainer() { var buil...
Paulo Jardim
1

votes
0

answer
47

Views

Multiple Implementation of service interface for different API controllers Autofac

I have many api controllers that have IService and ILog interface, and some more as constructor parameter. IService have different implementation: public interface IService { void Process(); } DefaultService : IService and CallService : IService as an example. I created two api controllers, DefaultC...
ash

View additional questions