autofac

Using Autofac to inject a dependency into the Main entry point in a console app

邮差的信 提交于 2019-11-27 03:34:51
问题 Say I have a simple console application: public static class Program { private static ILog Log { get; set; } public static void Main() { Log.Write("Hello, world!"); } } What is the simplest way I can use Autofac to inject the Log instance and ensure that the Log property will not be null at run time? The issue is that I can't pass it in through Main() and I'm trying to avoid service location using the container (just because). 回答1: What you should do is extract all logic from your main into a

Autofac: Batch registration of open-generic types

余生长醉 提交于 2019-11-27 01:53:23
问题 I got an assembly with many concrete types that implement IHandler<TCommand> , such as the following: public class MoveCustomerHandler : IHandler<MoveCustomerCommand> { void IHandler<MoveCustomerCommand>.Handle(MoveCustomerCommand c) { // some business logic for moving a customer. } } Currently, I'm registering them one by one as follows: builder.RegisterType<MoveCustomerHandler>() .As<IHandler<MoveCustomerCommand>>(); builder.RegisterType<ProcessOrderHandler>() .As<IHandler

Autofac: Register component and resolve depending on resolving parent

流过昼夜 提交于 2019-11-26 23:22:26
问题 I'm wanting to register a component to resolve with parameters based on the class that it might be resolving for. (That sounds a bit confusing, so I'll show an example). Here's an object that uses a logger: class MyObject : IMyObject { public ILogger Logger; public MyObject(ILogger logger) { Logger = logger; } } Now the logger that is passed in COULD be different from class to class. So I've got a rather patched idea for how to do that below: class MyLogger : ILogger { public string Name{get;

Resolving Generic Interface with Autofac

ぐ巨炮叔叔 提交于 2019-11-26 22:41:52
问题 Given the following code, how do I resolve the right SomeInstance in autofac? public class BaseClass {} public class SubClass1 : BaseClass {} public class SubClass2 : BaseClass {} public interface IGenericInterface<T> where T : BaseClass {} public class SomeInstance1<T> : IGenericInterface<T> where T : SubClass1 public class SomeInstance2<T> : IGenericInterface<T> where T : SubClass2 I want to choose SomeInstance1 or 2 based on the type of of the generic on the sub classes. So for example I

Get a list of all registered objects implementing a certain interface

两盒软妹~` 提交于 2019-11-26 20:00:01
问题 Consider the following builder.Register(c => new A()); builder.Register(c => new B()); builder.Register(c => new C()); B and C are both ISomeInterface . I would now like to get an IEnumerable of all registered objects that implement ISomeInterface . How can I accomplish this in Autofac? 回答1: Just tried this, works and does not depend on lifetime context: Enumerate types using Activator instead var types = con.ComponentRegistry.Registrations .Where(r => typeof(ISomeInterface).IsAssignableFrom

Register Generic Type with Autofac

陌路散爱 提交于 2019-11-26 19:50:22
问题 I have UnitofWork class and it implement IUnitOfWork. I try to register that with autofac : var builder = new ContainerBuilder(); builder .RegisterGeneric(typeof(UnitOfWork<Repository<>,>)) .As(typeof(IUnitOfWork)) .InstancePerDependency(); Implementation is: public class UnitOfWork<T, O> : IUnitOfWork where T : Repository<O> where O : BaseEntity { } public interface IUnitOfWork : IDisposable { void SaveChanges(); } Gives an error "Type expected" but this one work on another project: public

How to set ViewBag properties for all Views without using a base class for Controllers?

梦想的初衷 提交于 2019-11-26 19:22:30
In the past I've stuck common properties, such as the current user, onto ViewData/ViewBag in a global fashion by having all Controllers inherit from a common base controller. This allowed my to use IoC on the base controller and not just reach out into global shared for such data. I'm wondering if there is an alternate way of inserting this kind of code into the MVC pipeline? Nicholas Blumhardt Un-tried by me, but you might look at registering your views and then setting the view data during the activation process. Because views are registered on-the-fly, the registration syntax doesn't help

Does Ninject support Func (auto generated factory)?

我的梦境 提交于 2019-11-26 17:35:07
Autofac automatically generates factories for Func<T> ; I can even pass parameters. public class MyClass { public MyClass(Func<A> a, Func<int, B> b) { var _a = a(); var _b = b(1); } } Can I do the same with Ninject? If not, what workaround can I apply? Thanks. Update : Just found this post, seems the answer is no: How do I handle classes with static methods with Ninject? Remo Gloor NB Ninject 3.0 and later has this fully supported using the Ninject.Extensions.Factory package, see the wiki:- https://github.com/ninject/ninject.extensions.factory/wiki EDIT: NB there is a Bind<T>().ToFactory()

MVC Web API not working with Autofac Integration

给你一囗甜甜゛ 提交于 2019-11-26 16:54:07
问题 I used the MVC integration from autofac like this: ... var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); But now I want to recreate the solution with the new Web Api RTM. And I want to use the new AutofacWebApiDependencyResolver class. But if I do this with AutofacWebApiDependencyResolver i got this error: The type Autofac.Integration.WebApi.AutofacWebApiDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation

Dependency Injection and IDisposable

99封情书 提交于 2019-11-26 16:28:11
问题 I'm a little bit confused about Dispose() methods in IDisposable implementations with Autofac usage Say I have a certain depth to my objects: Controller depends on IManager ; Manager depends on IRepository ; Repository depends on ISession ; ISession is IDisposable . This leads to the following object graph: new Controller( new Manager( new Repository( new Session()))); Do I need to make my Manager and Repository implement IDisposable as well and call Manager.Dispose() in Controller,