autofac

How to pass controller's ModelState to my service constructor with Autofac?

点点圈 提交于 2019-12-18 04:17:20
问题 I have a wrapper on ModelStateDictionary which all my services accept. Is it possible to configure the autofac to inject the controller ModelStateDictionary into the constructor of the wrapper and then inject it into service constructor? //code public class ModelValidation : IModelValidation { public ModelValidation(ModelStateDictionary msd){...} .. .. } public class CustomerService{ public CustomerService(IModelValidation mv){...} .. } Thanks 回答1: Based on your comments I hereby revise my

Resolve instance - Autofac

北城以北 提交于 2019-12-18 04:14:37
问题 I'm trying to figure out how to resolve a instance somewhere in the code. At the application startup I registered a type static void Main() { var builder = new ContainerBuilder(); builder.RegisterType<Foo>().As<IFoo>(); } Now, how can I resolve an instance somewhere in the code ? In StructureMAP there is a static object ObjectFactory.GetInstance<IFoo>() 回答1: Read up on Getting Started. It should get you started. First off, what you are looking for is the container . Build it from the

Have to register every class before the autofac container can resolve?

那年仲夏 提交于 2019-12-18 04:01:39
问题 let's say this scenario: public class B {}; public class C { public C(B b){} } To resolve C from Autofac container, I have to register both B and C to container. But, today I used Unity, it seems I just need to register B to container, then C can be resolved. So Autofac can't do as Unity do? 回答1: With out-of-the-box Autofac it is expected that every type you want to use is registered with the container, either directly using the Register... methods or in bulk using RegisterAssemblyTypes . But

Autofac dependency injection in implementation of OAuthAuthorizationServerProvider

≯℡__Kan透↙ 提交于 2019-12-18 03:10:14
问题 I am creating a Web Api application and I want to use bearer tokens for the user authentication. I implemented the token logic, following this post and everything seems to work fine. NOTE: I am not using the ASP.NET Identity Provider. Instead I have created a custom User entity and services for it. public class Startup { public void Configuration(IAppBuilder app) { ConfigureOAuth(app); var config = new HttpConfiguration(); var container = DependancyConfig.Register(); var dependencyResolver =

Register global filters in ASP.Net MVC 4 and Autofac

旧城冷巷雨未停 提交于 2019-12-17 22:36:01
问题 I have a filter like this one: public class CustomFilterAttribute : ActionFilterAttribute, IAuthorizationFilter { public MyPropery Property { get; set; } .... } I need it to be run for every actions in my project I tried to register it in the GlobalFilters but my property doesn't get injected I tried This solution to register my filter but it doesn't get called Any idea on how to do that? 回答1: There's a new way of registering MVC global filters in AutoFac. First, remove the filter

Autofac lazy property injection

心不动则不痛 提交于 2019-12-17 21:23:33
问题 I'm trying to inject business logic implementations into web API base controller. Somehow property in base controller is always null . Also how can I do lazy injection? Startups.cs public IServiceProvider ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterType<ViewBusinessLogic>().As<IViewBusinessLogic>(). PropertiesAutowired(); containerBuilder.Populate(services); var container

'Autofac Circular Component Dependency Detected' Error

北慕城南 提交于 2019-12-17 20:24:08
问题 I am new to IoC and am using Autofac in my current project. I have the following 2 classes: public class UserService : IUserService { private readonly IUserRepository _repo; private readonly IMailService _mailService; public UserService(IUserRepository repo, IMailService mailService) { _repo = repo; _mailService = mailService; } } public class MailService : IMailService { private readonly IMailRepository _repo; private readonly IUserService _userService; public MailService(IMailRepository

Autofac in web applications, where should I store the container for easy access?

一笑奈何 提交于 2019-12-17 18:24:56
问题 I'm still pretty new to using Autofac and one thing I miss in the documentation and examples is how to make it easy to get to the configured container from different places in a web application. I know I can use the Autofac controller factory to automatically resolve constructor injected dependencies for controllers, but how about the other stuff you might need to resolve that is not injected yet. Is there an obvious pattern I am not aware of for this? Thank you! 回答1: First of all try not to

DbContext has been disposed and autofac

别来无恙 提交于 2019-12-17 18:07:24
问题 I have a controller: private readonly ILogger _logger; private readonly IRepository _repository; public HomeController(ILogger logger, IRepository repository) { _logger = logger; _repository = repository; } This is the repository: public class EfRepository : IRepository { // ...methods for add, delete, update entities // .... public void Dispose() { if (this._context != null) { this._context.SaveChanges(); (this._context as IDisposable).Dispose(); this._context = null; } } } Finally,

Autofac - The request lifetime scope cannot be created because the HttpContext is not available - due to async code?

浪尽此生 提交于 2019-12-17 16:34:22
问题 Short Question: Same as this unanswered problem Long Question: I just ported some code over from an MVC 4 + Web Api solution that was using Autofac into my new solution which is also using Autofac but only with Web Api 2 (no MVC 5.1 project, just a web api). In my previous solution I had MVC4 and Web Api so I had 2 Bootstrapper.cs files, one for each. I copied over just the Web Api bootstrapper for the new project. Now I have 2 other projects in the new solution that need to pull a dependency