autofac

Autofac “action injection” with ASP.NET MVC model binding

不打扰是莪最后的温柔 提交于 2019-12-12 03:15:21
问题 I'm using Autofac with ASP.NET MVC and am wondering if my view model setup in the web project is a good approach or not. In the past I only used constructer injection at the Controller level but thought it would be interesting to see if everything could be injected via Autofac. Let's say I have a view model that looks this: public class CollegeViewModel : BaseViewModel { private CollegeModel _model { get; set; } public int Id { get; set; } public string Name { get; set; } public

Autofac Exception: Could not load type from assembly

雨燕双飞 提交于 2019-12-12 03:04:12
问题 Issue While building an Autofac Container, upon registering an interface implementation, Autofac would throw an exception: Registration: protected override void Load(ContainerBuilder builder) { var assembly = typeof(MyModule).Assembly; builder.RegisterAssemblyTypes(assembly) .Where(x => x.Name.EndsWith("Procedure") .AsImplementedInterfaces(); base.Load(builder); } Error: Could not load type from assembly Attempted Solutions: Clean build / Rebuild Deleting bin/obj directories Changing manner

Token Based Authentication using ASP.NET Web API 2 and Owin throws 401 unauthorized

喜你入骨 提交于 2019-12-12 02:43:37
问题 I have create a OAuth Authentication using the guide from Taiseer Joudeh. I have created an endpoint /token to make the authentication. It works and I receive a result like this. { "access_token": "dhBvPjsHUoIs6k8NDsXfROpTq63qlww_7Bifl0LOzIxhZnngld0QCU-x4q4Qa7xWhhIQeQbbK6gYu_hLIYfUbsFMsdXwqlOqAYabJHNNsnJPMMHNADb-KCQznPQy7-waaqKMCVH1HPqx4L30sXlX0L8MbjtrtkX9-jxHaWdPapqYA9lU4Ai2-Z5-zXxoriFDL-SvxrUnBTDQMnRxOH_oEyclUngzW-is543TtJ0bysQ", "token_type": "bearer", "expires_in": 86399 } But if I use

Is it possible to create a new scope whenever a component is resolved?

半世苍凉 提交于 2019-12-12 02:39:11
问题 I am trying to add autofac to a legacy WinForms App to manage dependencies and make it more testable. I was wondering whether it is possible to create a new LifeTimeScope every time a Func is injected? I currently have: public static TForm ResolveFormWithScope<TForm>(this ILifetimeScope self) where TForm : Form { ILifetimeScope formScope = self.BeginLifetimeScope("FormScope"); var form = formScope.Resolve<TForm>(); form.Closed += (s, e) => formScope.Dispose(); return form; } But ideally would

Call Initialize after simple ctor and after MVC parameters are bound

▼魔方 西西 提交于 2019-12-12 02:04:55
问题 I am using autofac and MVC together to allow me to auto resolve view models. This works well although I have an issue where I need to call an Initializable method for every view model after it is resolved. I solved this issue by simply hooking into the OnActivated method within autofac. The only issue here is that the Initialize method is called before the MVC has bound its parameters to the properties within the view model, therefore resulting in partial initialization. I can solve this by

Autofac Resolve using delegate factory by type

佐手、 提交于 2019-12-12 01:51:58
问题 I am using Autofac for IoC in my project. Due to some legacy software libraries I must pass some services to the controller that can't be resolved, and must be passed as parameter. I've made a generic control using delegate factories like this: public MyClass<TController, TInterface> { private delegate TController ControllerFactory(TInterface service); protected TController _myController; protected TController Controller { get { return _controller ?? (_controller = ServiceLocator.Resolve

Autofac. Register class with constructor with params

拟墨画扇 提交于 2019-12-12 01:39:53
问题 I have the class custom class: public class MyClass: IMyClass{ public MyClass(String resolverNamespace){ //Do some stuff dependent on resolverNamespace } //... Other stuff } public interface IMyClass{ //... Other stuff } I need to register it in my autofac container to allow resolve instance depends on the namespace of caller. Thats how I expect it should be: //Registering in autofac builder.Register(x=>new MyClass(x.ResolveCallerNamespace)).As(IMyClass); // Somewhere in application namespace

Autofac Injection of data into OWIN Startup class

非 Y 不嫁゛ 提交于 2019-12-11 23:25:59
问题 I've been looking around on SO for a while and can't find the answer to my particular question: How do you use Autofac to inject data (via constructor or property) into the OWIN Startup class? I'm using Autofac in a Windows service which also handles creation of back-end services etc. so I'm doing all the configuration reading and container building in there. I want to be able to register the Startup class so I can inject allowed origins for CORS but even when I register the object with a

MVC4/AutoFac dependency injection in ViewModel

喜欢而已 提交于 2019-12-11 22:52:37
问题 I have DI implemented using the constructor pattern in our MVC4 application, it's been working perfectly for a couple of years now. Recently, we had a contractor come in the do some work. He needed to access our services layer in a view model and hard coded the services (and their repositories) in the constructor of the viewModel, bad, bad, bad. What he did was basically mess up our loose coupling we worked our buts off to implement and maintain. Timing right now prevents us from re-writing

How to specify what type I need to register to specific type by Autofac

孤人 提交于 2019-12-11 20:30:59
问题 I want to specify type of weapon for different warriors public interface IWarrior { string Kill(); } public interface IWeapon { string KillSound(); } public class Zombie : IWarrior { private readonly IWeapon _weapon; public Zombie(IWeapon weapon) { _weapon = weapon; } public string Kill() { return _weapon.KillSound(); } } public class Soldier : IWarrior { private readonly IWeapon _weapon; public Soldier(IWeapon weapon) { _weapon = weapon; } public string Kill() { return _weapon.KillSound(); }