autofac

How to resolve dependency in dependency register time in Autofac

落花浮王杯 提交于 2020-06-23 05:37:50
问题 earlier I used Microsoft unity IOC. Unity container has RegisterType & Resolve method. So I able to resolve same interface with multiple implementation. but latest autofac (var builder = new ContainerBuilder();) builder hasn't resolve() method and we can't manually build (builder.Build();). Anyone has any idea, how to convert below code base to autofac or how to use resolve method with dependency registration. container.RegisterType<IQueryManager, FirstManager>(new TypedParameter(typeof

how to pass parameters on resolve time in autofac

妖精的绣舞 提交于 2020-06-14 06:29:43
问题 I write following register type in autofac: builder.RegisterType<NoteBookContext>() .As<DbContext>() .WithParameter(ResolvedParameter.ForNamed<DbContext>("connectionstring")); In fact I write this code for injecting NoteBookContext with a connectionstring parameter. (ie : new NoteBookContext(string connectionstring) ) Now , How can I Pass value of parameter at runtime? 回答1: The WithParameter method has a overload that accept delegate for dynamic instanciation. The first argument is a

how to pass parameters on resolve time in autofac

不羁的心 提交于 2020-06-14 06:28:06
问题 I write following register type in autofac: builder.RegisterType<NoteBookContext>() .As<DbContext>() .WithParameter(ResolvedParameter.ForNamed<DbContext>("connectionstring")); In fact I write this code for injecting NoteBookContext with a connectionstring parameter. (ie : new NoteBookContext(string connectionstring) ) Now , How can I Pass value of parameter at runtime? 回答1: The WithParameter method has a overload that accept delegate for dynamic instanciation. The first argument is a

How can I add a custom JSON file into IConfiguration?

末鹿安然 提交于 2020-05-26 10:49:25
问题 I'm using asp.net + Autofac. I'm trying to load a custom JSON configuration file, and either create/instance an IConfiguration instance based on that, or at least include my file into whatever IConfiguration asp.net builds by default. My problem is that asp.net seems to override the dependency registration for IConfiguration. I can't seem to register my own IConfiguration object - the DI container will always resolve this to some instance that seems to have been generated by the asp.net

How can I add a custom JSON file into IConfiguration?

╄→гoц情女王★ 提交于 2020-05-26 10:49:00
问题 I'm using asp.net + Autofac. I'm trying to load a custom JSON configuration file, and either create/instance an IConfiguration instance based on that, or at least include my file into whatever IConfiguration asp.net builds by default. My problem is that asp.net seems to override the dependency registration for IConfiguration. I can't seem to register my own IConfiguration object - the DI container will always resolve this to some instance that seems to have been generated by the asp.net

Autofac automate assembly scanning .net core 3.0 - Register according to Lifetime

放肆的年华 提交于 2020-05-23 21:11:28
问题 I realize this question is asked a lot. But I cannot figure out the solution to this problem. The thing i am trying to resolve is create three seperate interfaces (Singleton Scoped, InstancePerRequest). And register all the services under their implemented interface, without having to manually add them to DI container. Autofac Scanning Assemblies for certain class type Autofac register assembly types Autofac assembly scanning - .NET Core Before you say duplicate hear me out. I have provided a

Autofac ResolvedParameter/ComponentContext not working

你离开我真会死。 提交于 2020-05-22 10:05:07
问题 I am using asp.net core along with Entity Framework Core . My scenario here is, I want to change the connection string at runtime based on HttpContext query string value. I am trying to pass ResolvedParameter with Reflection components as documented. But, It is not getting registered when I resolve this. Here below, I have attached my code snippet. Autofac Registration class: public class DependencyRegistrar : IDependencyRegistrar { public virtual void Register(ContainerBuilder builder) {

How to register open generic with custom factory method?

与世无争的帅哥 提交于 2020-05-15 09:53:05
问题 TL;DR : Can I create a generic factory with Autofac, so that I can inject IProduct<TModel> rather than resolving it from IFactory anywhere I need it? Is there a way to move the resolving-from-factory task to the composition root? So I'm using a third party library, which exposes some generic interfaces which are created through a factory. For demonstration purposes, we'll assume that the following code is the library: Third party library mock-up : public interface IFactory { IProduct<TModel>

Autofac resolve the same class instance with different parameters

谁说我不能喝 提交于 2020-05-15 02:32:06
问题 I have a Xamarin.Forms application where I am using Autofac for DI. SO what I want is quite simple but I am unable to do something which seems like a sinch to do. So I have a class that has two constructors both of them have a single parameter and based on certain conditions I want to resolve either one. As an example, I have a class A and what I want is based on conditions B or C should be resolved. Eg: public class A { public A(B bObject) {...} public A(C cObject) {...} } But I am unable to

Autofac resolve the same class instance with different parameters

廉价感情. 提交于 2020-05-15 02:32:03
问题 I have a Xamarin.Forms application where I am using Autofac for DI. SO what I want is quite simple but I am unable to do something which seems like a sinch to do. So I have a class that has two constructors both of them have a single parameter and based on certain conditions I want to resolve either one. As an example, I have a class A and what I want is based on conditions B or C should be resolved. Eg: public class A { public A(B bObject) {...} public A(C cObject) {...} } But I am unable to